Javascript 嵌套保证了并行效果?

Javascript 嵌套保证了并行效果?,javascript,promise,ecmascript-6,Javascript,Promise,Ecmascript 6,我必须在承诺中包含承诺,这可以吗,或者被认为是不好的做法 我有一个类,它有一个方法,fetchArticles,fetchImages,和main。 main是调用fetchArticles+fetchImages fetchArticles从另一个文件运行一个函数,该函数返回一个承诺,但我也在fetchArticles类方法本身上返回一个承诺,因此当它获取文章时,它将继续获取图像 fetchImages方法未承诺,但从另一个文件调用承诺的函数 我不确定这是否是实现parralel效果的最佳

我必须在承诺中包含承诺,这可以吗,或者被认为是不好的做法

  • 我有一个类,它有一个方法,
    fetchArticles
    fetchImages
    ,和
    main
    • main
      是调用
      fetchArticles
      +
      fetchImages
    • fetchArticles
      从另一个文件运行一个函数,该函数返回一个承诺,但我也在
      fetchArticles
      类方法本身上返回一个承诺,因此当它获取文章时,它将继续获取图像
    • fetchImages
      方法未承诺,但从另一个文件调用承诺的函数
  • 我不确定这是否是实现parralel效果的最佳方法

    main () {
        // Call the promised fetchArticles class method
        this.fetchArticles()
        .then ( () => this.fetchImages( () => {
             this.res.json(this.articles.data);
        }));
    }
    
    
    fetchArticles () {
        return new Promise ((fullfil, rej) => {
          // Calling a promised function which simply returns an array of articles
          fetchArticles (this.parametersForApiCall)
           .then ( (data) => {
            this.articles.data = data;
            this.articles.imageIds = [1,5,2,8,99,1000,22,44,55,120,241]; // Extract image IDS and save to class this
            fullfil();
          })
          .catch ( (err) => {
            console.log ("Something went wrong with api call", err);
            res.json({error: "Something went wrong", code: 1011});
            reject();
          });
        });
      }
    
    fetchImages (cb) {
          // Calling a promised function which simply returns an array of images
        fetchImages (this.imageIds).then( (imgs) => {
          this.images = imgs;
          cb (); // Temp callback method
        }).catch ( (err) => {
          console.log (err, "error finding images in then")
        })
      }
    }
    
    我应该使用类似异步并行的东西吗?注意:我在
    获取图像
    方法中临时添加了一个
    回调
    ,直到找到链接承诺的好解决方案。

    似乎是的工作。

    似乎是的工作。

    注意:

  • 您在
    fetchArticles
    函数中创建了一个不必要的承诺。您可以直接返回promisified fetchArticles的结果

  • 使用将允许您同时发射两个项目。如果这两种方法互不依赖,那么这是一种很好的方法。我已经用该代码更新了
    main
    函数

  • 同样,您可以在
    fetchImages
    函数中直接返回承诺。因为这也是承诺,所以您不再需要回调。我把它拿走了

  • 结果代码

    main () {
        // Call the promised fetchArticles and fetchImages class methods
        Promise.all([this.fetchArticles(), this.fetchImages()])
          .then(() => this.res.json(this.articles.data));
    }
    
    
    fetchArticles () {
      // Calling a promised function which simply returns an array of articles
      return fetchArticles (this.parametersForApiCall)
        .then ( (data) => {
          this.articles.data = data;
          this.articles.imageIds = [1,5,2,8,99,1000,22,44,55,120,241]; // Extract image IDS and save to class this
        })
        .catch ( (err) => {
          console.log ("Something went wrong with api call", err);
          res.json({error: "Something went wrong", code: 1011});
          reject();
        });
    }
    
    fetchImages () {
        // Calling a promised function which simply returns an array of images
        return fetchImages (this.imageIds).then( (imgs) => {
          this.images = imgs;
        }).catch ( (err) => {
          console.log (err, "error finding images in then")
        })
      }
    }
    
    请注意:

  • 您在
    fetchArticles
    函数中创建了一个不必要的承诺。您可以直接返回promisified fetchArticles的结果

  • 使用将允许您同时发射两个项目。如果这两种方法互不依赖,那么这是一种很好的方法。我已经用该代码更新了
    main
    函数

  • 同样,您可以在
    fetchImages
    函数中直接返回承诺。因为这也是承诺,所以您不再需要回调。我把它拿走了

  • 结果代码

    main () {
        // Call the promised fetchArticles and fetchImages class methods
        Promise.all([this.fetchArticles(), this.fetchImages()])
          .then(() => this.res.json(this.articles.data));
    }
    
    
    fetchArticles () {
      // Calling a promised function which simply returns an array of articles
      return fetchArticles (this.parametersForApiCall)
        .then ( (data) => {
          this.articles.data = data;
          this.articles.imageIds = [1,5,2,8,99,1000,22,44,55,120,241]; // Extract image IDS and save to class this
        })
        .catch ( (err) => {
          console.log ("Something went wrong with api call", err);
          res.json({error: "Something went wrong", code: 1011});
          reject();
        });
    }
    
    fetchImages () {
        // Calling a promised function which simply returns an array of images
        return fetchImages (this.imageIds).then( (imgs) => {
          this.images = imgs;
        }).catch ( (err) => {
          console.log (err, "error finding images in then")
        })
      }
    }
    

    嵌套的承诺很好:)嵌套的承诺很好:)我决定接受你的承诺,因为它更详细!当然可以看看我的解释性编辑,其他一些可能会有用的信息。我决定接受你的,因为它更详细!当然可以看看我的解释性编辑,了解一些其他可能有用的信息。