等待 busboy on #file 完成,然后再调用 on#finish

Wait for busboy on#file to finish before calling on#finish

本文关键字:然后 调用 on#finish 完成 busboy on #file 等待      更新时间:2023-09-26

好吧,我有这个:

req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
    core.upload(filename, file, function(key2) {
        if (key2 != null) {
            key = key2;
        }
        console.log("KEY SET");
        file.resume();
    });
});

还有一个完成功能:

req.busboy.on('finish', function () {
    console.log("FINISH FIRE");
}

我遇到的问题是FINISH FIRE首先触发,并且仅在上传KEY SET触发后触发,并且我在设置数据时无法嵌套函数:

req.busboy.on('field', function (key, value, keyTruncated, valueTruncated) {
    //setting data
}

所以有这个:

req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
    core.upload(filename, file, function(key2) {
        if (key2 != null) {
            key = key2;
        }
        file.resume();
        runFinishFunction();
    });
});

真的行不通,因为我不知道 on#field 是否已经完成。

你有一个函数和这样的变量

var finishOne, FinishTwo
finish(finish1, finish2) {
     if (finishOne != true && finish1 != null) finishOne = finish1;
     if (finishTwo != true && finish2 != null) finishTwo = finish2;
     if (finishOne && FinishTwo) {
         //RUN CODE
     }
}

然后你可以在方法中使用它,并调用一个:

finish(true, null) And the other one with finish(null, true)

它看起来不是一个好的解决方案,所以欢迎更好的解决方案