1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-03 08:35:29 +02:00
joplin/ReactNativeClient/lib/promise-utils.js
Laurent Cozic 55c5ddedf4 Revert "Applied prettier to code base"
This reverts commit c4f19465a6.
2018-03-09 20:59:12 +00:00

37 lines
698 B
JavaScript

function promiseChain(chain, defaultValue = null) {
let output = new Promise((resolve, reject) => { resolve(defaultValue); });
for (let i = 0; i < chain.length; i++) {
let f = chain[i];
output = output.then(f);
}
return output;
}
function promiseWhile(callback) {
let isDone = false;
function done() {
isDone = true;
}
let iterationDone = false;
let p = callback(done).then(() => {
iterationDone = true;
});
let iid = setInterval(() => {
if (iterationDone) {
if (isDone) {
clearInterval(iid);
return;
}
iterationDone = false;
callback(done).then(() => {
iterationDone = true;
});
}
}, 100);
}
module.exports = { promiseChain, promiseWhile };