我正在使用具有不带回调方法的node.js模块。取而代之的是,当该方法完成时会触发事件。我想解决一个承诺,使用该事件作为回调确保我的方法已成功完成。等待事件以解决承诺
array.lenght在承诺可以是X.所以,我需要'听'X次事件,以确保所有方法已成功完成< - 这不是问题,我只是告诉你,我知道这可能发生
事件:
tf2.on('craftingComplete', function(recipe, itemsGained){
if(recipe == -1){
console.log('CRAFT FAILED')
}
else{
countOfCraft++;
console.log('Craft completed! Got a new Item #'+itemsGained);
}
})
承诺:
const craftWepsByClass = function(array, heroClass){
return new Promise(function (resolve, reject){
if(array.length < 2){
console.log('Done crafting weps of '+heroClass);
return resolve();
}
else{
for (var i = 0; i < array.length; i+=2) {
tf2.craft([array[i].id, array[i+1].id]); // <--- this is the module method witouth callback
}
return resolve(); // <---- I want resolve this, when all tf2.craft() has been completed. I need 'hear' event many times as array.length
}
})
}
'tf2.craft()'是否返回'Promise'?请注意,一个'Promise'只能解决或拒绝一次。 – guest271314
@ guest271314不要。 tf2.craft()不返回任何内容; –
'有一个没有回调的方法.''很好''是一个回调.. :) – Keith