Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为该场景编写Promise.all_Javascript_Es6 Promise - Fatal编程技术网

Javascript 如何为该场景编写Promise.all

Javascript 如何为该场景编写Promise.all,javascript,es6-promise,Javascript,Es6 Promise,我正在试图找出如何为这个场景编写promise.all。假设我们有一个学生数组。对于每个学生,我需要按顺序对Create和Update进行服务器调用(2次调用)。但我可以同时为所有学生打电话。在所有学生创建和更新完成后,我需要再次拨打REST电话。是用承诺,好吗?还是应该依次给每个学生打电话?下面是我的代码,但我认为它不正确 createAndUpdateStudents = () => { return this.students.map((student) =&g

我正在试图找出如何为这个场景编写promise.all。假设我们有一个学生数组。对于每个学生,我需要按顺序对Create和Update进行服务器调用(2次调用)。但我可以同时为所有学生打电话。在所有学生创建和更新完成后,我需要再次拨打REST电话。是用承诺,好吗?还是应该依次给每个学生打电话?下面是我的代码,但我认为它不正确

    createAndUpdateStudents = () => {
       return this.students.map((student) => {
           return create(student.name) //create returns a promise
                  .then((response) => {
                     return _.get(response, "id");
                  })
                  .then((studentId) => {
                     update(studentId, student.grade) //update returns a promise 
                  });
       });
    }

    save = () => {
       Promise.all(this.createAndUpdateStudents())
       .then(() => {
           //another rest call. This should happen only after all the students are updated
       })
    }

我写了一些简单的承诺,但我没能做到这一点

您不会从
返回
更新()
调用。然后()
Facepalm。谢谢。您不必从
.then()返回
更新()
调用。谢谢