Skip to content

Instantly share code, notes, and snippets.

@handicraftsman
Created May 18, 2018 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save handicraftsman/a2a365d07845a6256a86395b319d353f to your computer and use it in GitHub Desktop.
Save handicraftsman/a2a365d07845a6256a86395b319d353f to your computer and use it in GitHub Desktop.
asyncNew
module.exports = (obj, ...args) => {
return new Promise((res, rej) => {
try {
res(new obj(...args));
} catch (e) {
rej(e);
}
});
}
const asyncNew = require('./asyncNew');
asyncNew(Set, [1,2,3,4,5,6,7,8]).then(set => {
console.log(set);
});
@handicraftsman
Copy link
Author

I would like to see this in js as a part of syntax though:

// somewhere in an async function
let s = await async new Set([1,2,3,4,5,6,7,8]);
console.log(s);

// same as
(async new Set([1,2,3,4,5,6,7,8])).then(s => {
  console.log(s);
});

@handicraftsman
Copy link
Author

Well, await async new is verbose, but still a nice idea imo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment