为什么带有async / await的Array.map()返回奇怪的结果?

时间:2019-02-05 16:52:56

标签: javascript vue.js async-await arrow-functions

这是我代码的有效版本,可按预期返回所有内容:

#include <iostream>

using namespace std;


void findminval (double num1, double num2) {
double minval;
if (num1<num2){
    minval=num1;
} else {
    minval=num2;
}

}
void findminval (int num1, int num2) {
int minval;
if (num1<num2){
    minval=num1;
} else {
    minval=num2;
}

}

int main(int argc, char** argv) {
int num1,num2;
    cout<<"Enter Two Numbers"<<endl;
    cin>>num1>>num2;
    int minimumvalue = findminval(int num1,int num2);
    cout<<minimumvalue<<endl;

double num1,num2;
    cout<<"Enter Two Numbers"<<endl;
    cin>>num1>>num2;
    double minimumvalue = findminval(num1,num2);
    cout<<minimumvalue<<endl;
return 0;
}

这是返回错误结果的“残破”版本:

***.then(r => r.json()).then(async r => {

                    for (let i = 0; i < r.length; i++) {
                        let pipeline = r[i];
                        pipeline.collapsed = true;
                        pipeline.levels = await this.getPipelineLevels(pipeline.id);
                    }

                    this.project.pipelines.items = r;
                })

控制台中****.then(r => r.json()).then(r => { let pipelines = r.map(async (value) => { let levels = await this.getPipelineLevels(value.id); return {...value, collapsed: true, levels: levels}; }); this.project.pipelines.levels = pipelines; 之后出现console.log(JSON.stringify(pipelines))的奇怪输出:

*.map()

这是怎么回事?

2 个答案:

答案 0 :(得分:3)

由于Array.map实际上await并未传递给回调函数。不必担心您将其回调标记为async

只需Array.map您的承诺,然后将其传递给Promise.all,并让它等待您的一切(并行)。

const getPipelineLevels = id => new Promise(resolve => {
  setTimeout(() => resolve({ id: id, foo: 'bar' }), 500)
})

const idx = [1,2,3,4,5]

const tasks = idx.map(id => {
  return getPipelineLevels(id)
    .then(value => ({ ...value, bar: 'baz' }))
})

Promise.all(tasks)
  .then(results => {
    console.log(results)
  })

答案 1 :(得分:2)

尝试这样的事情:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processReleaseResources'.
> Android resource linking failed
  Output:  C:\Users\joseph.arriaza\Desktop\proyects\tagueoflutter\build\app\intermediates\incremental\mergeReleaseResources\merged.dir\values\values.xml:137: error: resource android:attr/fontVariationSettings not found.
  C:\Users\joseph.arriaza\Desktop\proyects\tagueoflutter\build\app\intermediates\incremental\mergeReleaseResources\merged.dir\values\values.xml:137: error: resource android:attr/ttcIndex not found.
  error: failed linking references.

  Command: C:\Users\joseph.arriaza\.gradle\caches\transforms-1\files-1.1\aapt2-3.2.1-4818971-windows.jar\42d72a25de05c0a46e75cddecb240d5d\aapt2-3.2.1-4818971-windows\aapt2.exe link -I\
          C:\Users\joseph.arriaza\AppData\Local\Android\sdk\platforms\android-27\android.jar\
          --manifest\
          C:\Users\joseph.arriaza\Desktop\proyects\tagueoflutter\build\app\intermediates\merged_manifests\release\processReleaseManifest\merged\AndroidManifest.xml\
          -o\
          C:\Users\joseph.arriaza\Desktop\proyects\tagueoflutter\build\app\intermediates\processed_res\release\processReleaseResources\out\resources-release.ap_\
          -R\
          @C:\Users\joseph.arriaza\Desktop\proyects\tagueoflutter\build\app\intermediates\incremental\processReleaseResources\resources-list-for-resources-release.ap_.txt\
          --auto-add-overlay\
          --java\
          C:\Users\joseph.arriaza\Desktop\proyects\tagueoflutter\build\app\generated\not_namespaced_r_class_sources\release\processReleaseResources\r\
          --custom-package\
          com.archangelsystems.tagueo\
          -0\
          apk\
          --output-text-symbols\
          C:\Users\joseph.arriaza\Desktop\proyects\tagueoflutter\build\app\intermediates\symbols\release\R.txt\
          --no-version-vectors
  Daemon:  AAPT2 aapt2-3.2.1-4818971-windows Daemon #0
  Output:  C:\Users\joseph.arriaza\.gradle\caches\transforms-1\files-1.1\core-1.0.0.aar\4889927745807dec05ed4fe733f46abb\res\values\values.xml:89:5-125:25: AAPT: error: resource android:attr/fontVariationSettings not found.

  C:\Users\joseph.arriaza\.gradle\caches\transforms-1\files-1.1\core-1.0.0.aar\4889927745807dec05ed4fe733f46abb\res\values\values.xml:89:5-125:25: AAPT: error: resource android:attr/ttcIndex not found.

  error: failed linking references.
  Command: C:\Users\joseph.arriaza\.gradle\caches\transforms-1\files-1.1\aapt2-3.2.1-4818971-windows.jar\42d72a25de05c0a46e75cddecb240d5d\aapt2-3.2.1-4818971-windows\aapt2.exe link -I\
          C:\Users\joseph.arriaza\AppData\Local\Android\sdk\platforms\android-27\android.jar\
          --manifest\
          C:\Users\joseph.arriaza\Desktop\proyects\tagueoflutter\build\app\intermediates\merged_manifests\release\processReleaseManifest\merged\AndroidManifest.xml\
          -o\
          C:\Users\joseph.arriaza\Desktop\proyects\tagueoflutter\build\app\intermediates\processed_res\release\processReleaseResources\out\resources-release.ap_\
          -R\
          @C:\Users\joseph.arriaza\Desktop\proyects\tagueoflutter\build\app\intermediates\incremental\processReleaseResources\resources-list-for-resources-release.ap_.txt\
          --auto-add-overlay\
          --java\
          C:\Users\joseph.arriaza\Desktop\proyects\tagueoflutter\build\app\generated\not_namespaced_r_class_sources\release\processReleaseResources\r\
          --custom-package\
          com.archangelsystems.tagueo\
          -0\
          apk\
          --output-text-symbols\
          C:\Users\joseph.arriaza\Desktop\proyects\tagueoflutter\build\app\intermediates\symbols\release\R.txt\
          --no-version-vectors
  Daemon:  AAPT2 aapt2-3.2.1-4818971-windows Daemon #0

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 8s
Running Gradle task 'assembleRelease'...
Running Gradle task 'assembleRelease'... Done                       8.7s
Gradle task assembleRelease failed with exit code 1

.then(async r => { let pipelines = await Promise.all(r.map(async (value) => { let levels = await this.getPipelineLevels(value.id); return {...value, collapsed: true, levels: levels}; })); this.project.pipelines.levels = pipelines; }); 返回一个Promises数组。

由于并行等待,因此该解决方案也比OP试图实现的解决方案要快。

请注意,您应该avoid awaiting a .then(…) chain