[英]Returning promises up a function chain
Below is the structure of some code that I have written.下面是我写的一些代码的结构。 I am attempting to load a number of files to a server using ajax and once all are complete, do some action.
我正在尝试使用 ajax 将多个文件加载到服务器,一旦所有文件都完成,请执行一些操作。
function func1(items){
const results = []
for (let i=0; i<items.length; i++) {
results[i] = func2();
}
Promise.all(results).then(response => some_action())
}
function func3(params) {
return new Promise((resolve, reject) => {
//ajax call here and resolve/reject
})
}
function func2(){
if(stuff){
return func3(some_params);
} else {
return func3(other_params);
}
}
Unfortunately, it is not working as I expected.不幸的是,它没有按我预期的那样工作。 The array
results
is not an array of promises, but an array of undefined. results
数组不是promise数组,而是undefined数组。 I am new to Javascript promises, so any help would be appreciated.我是 Javascript 承诺的新手,所以任何帮助将不胜感激。
EDIT: to respond to the comment about the possibility of a silent return, I post the actual code for func2 (lightly modified):编辑:为了回应关于无声返回可能性的评论,我发布了 func2 的实际代码(稍作修改):
function func2(item, id, id2, top_level, path){
if(item.isFile){
item.file(function(file) {
if(file.name.substring(file.name.lastIndexOf('.')+1) === "docx"){
file = new File([file], file.name, {
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
})
}
if(file.name !== "desktop.ini"){
let url = 'url'
return func3(file, id, url, "", false);
}
});
} else {
window.parent.ipcRenderer.send('zip-folder', path);
window.parent.ipcRenderer.on('zipped', (event, buffer) => {
var zipped_file = new File([buffer], item.name + ".zip", {
type: "application/zip"
})
let url = "/url"
return func3(zipped_file, id, url, id2, true);
})
}
}
您需要在每个函数中返回新的 Promise !
Your else
block does not return anything:您的
else
块不返回任何内容:
...
else {
window.parent.ipcRenderer.send('zip-folder', path);
window.parent.ipcRenderer.on('zipped', (event, buffer) => {
var zipped_file = new File([buffer], item.name + ".zip", {
type: "application/zip"
})
let url = "/url"
return func3(zipped_file, id, url, id2, true);
})
// silently returns undefined
}
Don't be fooled by the return statement in the anonymous function.不要被匿名函数中的 return 语句所迷惑。 It returns the anonymous function, not
func2
:它返回匿名函数,而不是
func2
:
window.parent.ipcRenderer.on('zipped', return this
(event, buffer) => { <----------.
var zipped_file = new File([buffer], item.name + ".zip", { |
type: "application/zip" |
}) |
let url = "/url" |
return func3(zipped_file, id, url, id2, true); -------------------'
}
)
This would be more obvious if you rewrite your code without using anonymous functions:如果您在不使用匿名函数的情况下重写代码,这将更加明显:
function func4(id, url, id2) {
return function (event, buffer) {
var zipped_file = new File([buffer], item.name + ".zip", {
type: "application/zip"
})
let url = "/url"
return func3(zipped_file, id, url, id2, true);
}
}
function func2(item, id, id2, top_level, path){
if(item.isFile){
item.file(function(file) {
if(file.name.substring(file.name.lastIndexOf('.')+1) === "docx"){
file = new File([file], file.name, {
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
})
}
if(file.name !== "desktop.ini"){
let url = 'url'
return func3(file, id, url, "", false);
}
});
} else {
window.parent.ipcRenderer.send('zip-folder', path);
window.parent.ipcRenderer.on('zipped', func4(id, url, id2))
// no return statement !!
}
}
One way to get around it is to convert it into a promise:绕过它的一种方法是将其转换为承诺:
return new Promise((ok,fail) => {
window.parent.ipcRenderer.on('zipped', (event, buffer) => {
var zipped_file = new File([buffer], item.name + ".zip", {
type: "application/zip"
})
let url = "/url"
ok(func3(zipped_file, id, url, id2, true));
})
});
Of course, depending on how your error logic flows you may want to wrap the promise at a higher level.当然,根据您的错误逻辑如何流动,您可能希望在更高级别包装承诺。 This merely illustrates a quick fix.
这仅说明了快速修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.