在 promise.then 回调后执行操作
Perform an action after a promise.then callback
我试图在单个 Promise 中封装一些初始化/清理代码。如果执行一些代码,执行 then 然后再执行一些代码,我想要什么。这是我想出的:
function initialize() {
let callback;
console.log('intialization');
const promise = new Promise(resolve => callback = resolve);
new Promise(async () => {
await callback();
await promise;
console.log('cleanup');
});
return promise;
}
initialize().then(() => console.log('execute then'));
这在终端中给出了以下输出:
initialization
execute then
cleanup
- Promise {<fulfilled>: undefined}
到目前为止一切都很好。但是,当我们将回调设为异步时,它就不再起作用了。
initialize().then(
async () => {
await new Promise(resolve => {
setTimeout(
() => {
console.log('execute then');
resolve();
},
10000
)
})
}
);
给我这个输出:
initialization
cleanup
- Promise {<pending>}
execute then
我希望它看起来像这样:
initialization
- Promise {<pending>}
execute then
cleanup
我该如何解决这个问题?这有可能吗?
您可以接受定义异步操作的回调。然后它可以插入到一个承诺链的中间:
const delayMessage = (message, ms) =>
new Promise(resolve => setTimeout(() => {
console.log(message);
resolve();
}, ms));
async function somethingAsync() {
console.log('intialization');
}
function initialize(callback) {
return somethingAsync()
.then(callback)
.then(() => {
console.log('cleanup');
});
}
const middleOfProcess = () => delayMessage('execute then', 2000);
initialize(middleOfProcess);
即使中间有多个异步步骤,它也能正常工作,因为您可以简单地将它们链接在一起:
const delayMessage = (message, ms) =>
new Promise(resolve => setTimeout(() => {
console.log(message);
resolve();
}, ms));
async function somethingAsync() {
console.log('intialization');
}
function initialize(callback) {
return somethingAsync()
.then(callback)
.then(() => {
console.log('cleanup');
});
}
const middleOfProcess = () => delayMessage('execute then1', 2000)
.then(() => delayMessage('execute then2', 2000))
.then(() => delayMessage('execute then3', 2000));
initialize(middleOfProcess);
同样可以使用 async/await 语法来完成:
const delayMessage = (message, ms) =>
new Promise(resolve => setTimeout(() => {
console.log(message);
resolve();
}, ms));
async function somethingAsync() {
console.log('intialization');
}
async function initialize(callback) {
await somethingAsync();
await callback();
console.log('cleanup');
}
const middleOfProcess = async () => {
await delayMessage('execute then1', 2000);
await delayMessage('execute then2', 2000);
await delayMessage('execute then3', 2000);
};
initialize(middleOfProcess);
我试图在单个 Promise 中封装一些初始化/清理代码。如果执行一些代码,执行 then 然后再执行一些代码,我想要什么。这是我想出的:
function initialize() {
let callback;
console.log('intialization');
const promise = new Promise(resolve => callback = resolve);
new Promise(async () => {
await callback();
await promise;
console.log('cleanup');
});
return promise;
}
initialize().then(() => console.log('execute then'));
这在终端中给出了以下输出:
initialization
execute then
cleanup
- Promise {<fulfilled>: undefined}
到目前为止一切都很好。但是,当我们将回调设为异步时,它就不再起作用了。
initialize().then(
async () => {
await new Promise(resolve => {
setTimeout(
() => {
console.log('execute then');
resolve();
},
10000
)
})
}
);
给我这个输出:
initialization
cleanup
- Promise {<pending>}
execute then
我希望它看起来像这样:
initialization
- Promise {<pending>}
execute then
cleanup
我该如何解决这个问题?这有可能吗?
您可以接受定义异步操作的回调。然后它可以插入到一个承诺链的中间:
const delayMessage = (message, ms) =>
new Promise(resolve => setTimeout(() => {
console.log(message);
resolve();
}, ms));
async function somethingAsync() {
console.log('intialization');
}
function initialize(callback) {
return somethingAsync()
.then(callback)
.then(() => {
console.log('cleanup');
});
}
const middleOfProcess = () => delayMessage('execute then', 2000);
initialize(middleOfProcess);
即使中间有多个异步步骤,它也能正常工作,因为您可以简单地将它们链接在一起:
const delayMessage = (message, ms) =>
new Promise(resolve => setTimeout(() => {
console.log(message);
resolve();
}, ms));
async function somethingAsync() {
console.log('intialization');
}
function initialize(callback) {
return somethingAsync()
.then(callback)
.then(() => {
console.log('cleanup');
});
}
const middleOfProcess = () => delayMessage('execute then1', 2000)
.then(() => delayMessage('execute then2', 2000))
.then(() => delayMessage('execute then3', 2000));
initialize(middleOfProcess);
同样可以使用 async/await 语法来完成:
const delayMessage = (message, ms) =>
new Promise(resolve => setTimeout(() => {
console.log(message);
resolve();
}, ms));
async function somethingAsync() {
console.log('intialization');
}
async function initialize(callback) {
await somethingAsync();
await callback();
console.log('cleanup');
}
const middleOfProcess = async () => {
await delayMessage('execute then1', 2000);
await delayMessage('execute then2', 2000);
await delayMessage('execute then3', 2000);
};
initialize(middleOfProcess);