Open dw1284 opened 3 years ago
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
I went ahead and added async/await support myself and created a PR: https://github.com/uzairfarooq/arrive/pull/81
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
Thank you guys!
Not sure if this is possible, or if it is already implemented. It would be nice to use Async/Await with this library.
Example
Instead of this: