简体   繁体   English

循环内的承诺链

[英]Promise chain inside loop

I'm new in Javascript. 我是Java语言的新手。 I have a problem with promise chain. 我对诺言链有疑问。 I'm trying to execute a chain promise inside a for loop and I want an output like this: 我正在尝试在for loop内执行链承诺,我想要这样的输出:

one two three -------- one two three -------- one two three 一二三--------一二三--------一二三

But I always got an output like this 但是我总是得到这样的输出

one two ------- one two ------- one two ------- three three three 一二-------一二-------一二-------三三三

This is my code: 这是我的代码:

test(item: any, i: number){

    this.filePath = item.rows.item(i).filepath;
    this.fileName = this.filePath.substr(this.filePath.lastIndexOf('/') + 1);
    this.imgePath = this.filePath.replace(this.fileName, "");


    console.log(this.filePath)
    console.log(this.fileName)
    console.log(this.imgePath)

    const one = new Promise<boolean>(resolve => {
        this.readfile.checkFile(this.imgePath, this.fileName).then(isExists => {
            console.log("one")
            resolve(isExists);
        })
    });

    const two = one.then(isExists => {
        console.log("two")
        if (isExists) {
            return this.readfile.readAsDataURL(this.imgePath, this.fileName).then(res =>{
                return res;
            })
        }
        else {
            console.log("not exists")
        }
    })

    const three = two.then(res => {
        console.log("three")
        this.base64Image.push(res);
    })
}

process(item: any, rows: number) {
    let prom: Promise<void>[] = [];

    for (let i = 0; i < rows; i++) {
        this.test(item,i);
        console.log(i,"loop")
    }
}

Can anyone help me? 谁能帮我? I spend 8hrs and still I can't figure it out. 我花了8个小时,但仍然无法解决。 Thanks! 谢谢!

You are not waiting for the promise to complete before executing next item in the loop. 在执行循环中的下一项之前,您无需等待诺言完成。

for loop shouldn't be used with asynchronous code unless you want them to be executed paralelly or you are using async await . 除非您希望并行执行异步代码或正在使用async await否则不应将for循环与异步代码一起使用。

Changes needed: 需要更改:

Your test method should return promise, so that we can track when the promises are fulfilled. 您的测试方法应返回promise,以便我们可以跟踪何时实现promise。

You need to wait for the promise returned by previous test method to be fulfilled before executing next test method. 在执行下一个测试方法之前,您需要等待上一个测试方法返回的承诺得以实现。

test(item: any, i: number){

    this.filePath = item.rows.item(i).filepath;
    this.fileName = this.filePath.substr(this.filePath.lastIndexOf('/') + 1);
    this.imgePath = this.filePath.replace(this.fileName, "");


    console.log(this.filePath)
    console.log(this.fileName)
    console.log(this.imgePath)

    const one = new Promise<boolean>(resolve => {
        this.readfile.checkFile(this.imgePath, this.fileName).then(isExists => {
            console.log("one")
            resolve(isExists);
        })
    });

    const two = one.then(isExists => {
        console.log("two")
        if (isExists) {
            return this.readfile.readAsDataURL(this.imgePath, this.fileName).then(res =>{
                return res;
            })
        }
        else {
            console.log("not exists")
        }
    })

    const three = two.then(res => {
        console.log("three")
        this.base64Image.push(res);
    })

    return three;
}

process(item: any, rows: number) {
    let prom = Promise.resolve();

    for (let i = 0; i < rows; i++) {
        prom = prom.then(() => this.test(item,j));
        console.log(i,"loop")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM