One way to deal with asynchronous work like this is to use a callback function, eg:(处理此类异步工作的一种方法是使用回调函数,例如:)
function firstFunction(_callback){
// do some asynchronous work
// and when the asynchronous stuff is complete
_callback();
}
function secondFunction(){
// call first function and pass in a callback function which
// first function runs when it has completed
firstFunction(function() {
console.log('huzzah, I'm done!');
});
}
As per @Janaka Pushpakumara's suggestion, you can now use arrow functions to achieve the same thing.(按照@Janaka Pushpakumara的建议,您现在可以使用箭头功能实现相同的目的。)
For example:(例如:)
firstFunction(() => console.log('huzzah, I\'m done!'))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…