我在Promise的then()方法中执行Promsie.all(),然后首先执行Promsie.all()中的方法。代码如下

时间:2018-07-04 13:07:23

标签: javascript es6-promise

try {
        WebView.setWebContentsDebuggingEnabled(true);
    } catch (Exception ignored) {
        Crashlytics.logException(ignored);
    }

    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.getSettings().setDefaultTextEncodingName("utf-8");
    webView.setBackgroundColor(getResources().getColor(R.color.global_bg_front));
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); // fixes scrollbar on Froyo
    webView.setWebViewClient(customWebViewClient);
    webView.setWebChromeClient(new CustomWebChromeClient());

    webView.setOnKeyListener(new CustomOnKeyListener());

    webView.requestFocus(View.FOCUS_DOWN); // http://android24hours.blogspot.cz/2011/12/android-soft-keyboard-not-showing-on.html
    webView.setOnTouchListener(new CustomOnTouchListener());
    webView.setDownloadListener((url, userAgent, contentDisposition, mimetype, contentLength) -> {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    });
    // webview hardware acceleration
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

我在Promise的then()方法中执行Promsie.all(),然后首先执行Promsie.all()中的方法。代码如下 我在哪里出错,我的代码结果如下图所示 enter image description here

1 个答案:

答案 0 :(得分:1)

从代码的外观来看,我认为根本没有执行task数组函数中的代码。 Promise.all期望数组/ iterable对象是promises的精确对象,但是您只是将其传递给一个永远不会执行的函数数组。为了使该示例首先工作,您在task数组中的函数必须返回promise,其次,您必须调用这些函数。类似于:

task[0] = function() {
  return new Promise( function(resolve, reject) {
    $.ajax({
    url: url + 2,
    context: document.body,
    success: function(){
      console.log('02');
      resolve('success');
    }
  });
});

};

task[1] = function() {
  return new Promise(function(resolve, reject){
    $.ajax({
      url: url + 3,
      context: document.body,
      success: function(){
        console.log('03');
        resolve('success');
      }
    });
  });
};

task01().then(function(value) {
  const taskPromises = task.map((fn)=>fn());
  Promise.all(taskPromises);
});

请注意,您的代码有点混乱,请尝试编写更清晰的代码;