const myfunc = () => new Promise(resolve => resolve())
Is there anyone that actually likes this structure? I find it really hard to reason about what a line like this does. What is the upper limit on double arrows in one line? const myfunc = () => new Promise(resolve => resolve())
Is there anyone that actually likes this structure? I find it really hard to reason about what a line like this does. What is the upper limit on double arrows in one line? const wait = (ms = 500) => new Promise(resolve => setTimeout(resolve, ms));
then: async function () {
await wait(1000);
}
Perhaps it's hard to grok but it sure is a handy one-liner.Of course in newer versions of node we can now do:
import {
setTimeout,
} from 'timers/promises';
await setTimeout(100);const wait = require(‘util’).promisify(setTimeout)
async function process() {
return new Promise((resolve, reject) => {
emitter.on(”error”, (err) => reject(err));
emitter.on(”end”, (result) => resolve(result));
emitter.start();
});
}
async is not strictly needed here, but I find it a good practice to use it on functions which immediately return a Promise anyway. async function foo(bar) {
// ...
}
function nonObviousAsyncFn() {
// ...
return foo(quux);
}
Explicit return types would help, but most people don’t write them unless they’re forced by a linter. return await foo();