Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise and Async/Await Support #80

Open
dw1284 opened this issue Mar 10, 2021 · 4 comments
Open

Promise and Async/Await Support #80

dw1284 opened this issue Mar 10, 2021 · 4 comments

Comments

@dw1284
Copy link
Contributor

dw1284 commented Mar 10, 2021

Not sure if this is possible, or if it is already implemented. It would be nice to use Async/Await with this library.

Example

const element = await document.arrive('#elementId', {onceOnly: true, existing: true});
// do stuff with element

Instead of this:

document.arrive('#elementId', {onceOnly: true, existing: true}, function() {
  const element = this;
  // do stuff with element
});
@dw1284
Copy link
Contributor Author

dw1284 commented Mar 11, 2021

For anybody who wants to manually promisify the library, just insert this snippet somewhere in your code after your arrive.js import:

const objects = [HTMLElement, NodeList, HTMLCollection, HTMLDocument, Window];

const promisifiedArrive = function(a, b) {return new Promise((resolve) => {this.arriveWithCallback(a, b, resolve);})};
const promisifiedLeave = function(a, b) {return new Promise((resolve) => {this.leaveWithCallback(a, b, resolve);})};

objects.forEach(object => {
  object.prototype.arriveWithCallback = object.prototype.arrive;
  object.prototype.leaveWithCallback = object.prototype.leave;
  object.prototype.arrive = promisifiedArrive;
  object.prototype.leave = promisifiedLeave;
});

The following will now work:

const element = await document.arrive('#elementId', {onceOnly: true, existing: true});
// do stuff with element

@dw1284
Copy link
Contributor Author

dw1284 commented Mar 11, 2021

I went ahead and added async/await support myself and created a PR: #81

@tcaruth
Copy link

tcaruth commented Oct 22, 2021

For any future issue readers: the dev branch on this repo adds this functionality.

If you don't need to use npm, arrive.js(dev) contains these changes

@dmatora
Copy link

dmatora commented Jun 1, 2022

Thank you guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants