[英]How to make this code sequential without touching outer function
I need such sequence: 我需要这样的顺序:
🤡lurks in the shadows; 🤡在阴影中; the end 结束
How to achieve such result without touching 'sequential' function? 如何在不触及“顺序”功能的情况下实现这种效果?
Achieve sequential run 实现顺序运行
function who() {
return new Promise(resolve => {
setTimeout(() => {
resolve('🤡');
}, 200);
});
}
function what() {
return new Promise(resolve => {
setTimeout(() => {
resolve('lurks');
}, 300);
});
}
function where() {
return new Promise(resolve => {
setTimeout(() => {
resolve('in the shadows');
}, 500);
});
}
async function msg() {
const a = await who();
const b = await what();
const c = await where();
console.log(`${ a } ${ b } ${ c }`);
}
function sequential(){
msg();
console.log('the end');
}
sequential();
I need such sequence: 我需要这样的顺序:
🤡lurks in the shadows
the end
How to achieve such result without touching 'sequential' function? 如何在不触及“顺序”功能的情况下实现这种效果?
Current result is: 当前结果是:
the end
🤡lurks in the shadows
function who(data) { return new Promise(resolve => { setTimeout(() => { resolve(data + '🤡'); }, 200); }); } function what(data) { return new Promise(resolve => { setTimeout(() => { resolve(data + 'lurks'); }, 300); }); } function where(data) { return new Promise(resolve => { setTimeout(() => { resolve(data + 'in the shadows'); }, 500); }); } function msg() { return who('') .then(what) .then(where); } function sequential(){ return msg() .then((respo) => { console.log(JSON.stringify(respo)); console.log('the end'); }); } sequential();
You can do this! 你可以这样做!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.