Created
September 14, 2022 17:57
-
-
Save beatak/62ffef1ae8450e1c84572d707345694c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Runs funcs in serial, and returns each result | |
* | |
* @param {arr<Function(onFullFilled, onRejected)>} funcs - function to be executed | |
*/ | |
Promise.execSerial = function (funcs) { | |
return new Promise(async function (res, rej) { | |
const result = []; | |
for (let i = 0, len = funcs.length; i < len; ++i) { | |
try { | |
result.push(await (function () { | |
return new Promise((onFullFilled, onReject) => { | |
funcs[i](onFullFilled, onReject); | |
}); | |
})()); | |
} catch (e) { | |
const err = new Error(e.message); | |
err.stack = e.stack; | |
err.pendingResult = result; | |
return rej(err); | |
} | |
} | |
return res(result); | |
}); | |
}; | |
// /* EXAMPLE */ | |
// const result = await Promise.execSerial([ | |
// function (s, j) { | |
// s(0); | |
// }, | |
// function (s, j) { | |
// child_process.exec( | |
// 'sh -c "sleep 1 && false"', | |
// (err) => { | |
// if (err) { | |
// return j(err); | |
// } | |
// return s(1); | |
// } | |
// ); | |
// } | |
// ]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment