const waterfall = [
() => new Promise(res => setTimeout(() => res(1), 1100)),
prevResult => new Promise(res => setTimeout(() => res(prevResult + 1), 1000)),
];
async function callPromises(promises = []) {
let i = 0;
let result = Promise.resolve();
while (promises.length > i) {
const promiseFn = promises[i];
result = await promiseFn(result);
i += 1;
}
return result;
}
callPromises(waterfall).then(console.log);
Commands.findOne({ device: req.body.id })
.exec()
.then(cmd => {
if (!cmd) {
throw new Error('Not found');
}
return cmd;
})
.then(cmd => Commands.deleteOne({ _id: cmd._id }).exec().then(() => cmd))
.then(cmd =>
res.status(200).json({
commands: cmd.commands,
result: 'ok',
}),
)
.catch(err => {
console.log(err);
res.status(500).json({
error: err,
});
});
const { name, surname } = req.body;
const or = [{ name }];
const queryWhere = { [Op.or]: or };
if (surname) {
or.push({ surname });
}
find(queryWhere);
(async () => {
const arr = [1, 2, 3, 4, 5];
for (const item of arr) {
console.log('check: ' + item);
// тут бегаем за данными
const arr2 = await new Promise(res =>
setTimeout(
() => res(Array.from(Array(10)).map(() => Math.random())),
100,
),
);
if (arr2.length > 0) {
for (const item2 of arr2) {
console.log('item: ' + item2);
}
}
}
})();
Есть более простые аналоги, infinityScroll, etc