javascript
Promises sequence performing and parallel performnig with sequence result:
main.js
draft.js
onionskin.js
//@ts-check function promise1() { return new Promise((resolve => { console.log('promise1 started...'); setTimeout(() => resolve(1), 2000) })) } function promise2() { return new Promise((resolve => { console.log('promise2 started...'); setTimeout(() => resolve(2), 1000) })) } function promise3() { return new Promise((resolve => { console.log('promise3 started...'); setTimeout(() => resolve(3), 3000) })) } function waitAny(arr) { // Promise.all(arr.map(p => p())).then(r => {console.log(r); }) let promises = arr.map(p => p()); return { i: 0, next() { // if (arr[this.i]) return arr[this.i++]() if (promises[this.i]) { return promises[this.i++] } else { return Promise.resolve(null) } } } } let waitingList = waitAny([promise1, promise2, promise3]) async function run() { const error = null; while (!error) { const res = await waitingList.next().catch(er => { er = error; console.log(er); }) if (res) console.log(res) else { break; } } } run()
Новый листинг
скачать через терминал:
wget https://coding-style.ru/code_reviews/download/394
&& mkdir "Promises sequence performing"
&& unzip 394 -d ./"Promises sequence performing"
&& rm 394
&& cd "Promises sequence performing""
&& git init
(ваш голос учтен)