传递一个变量,在JavaScript异步函数(许诺) [英] Passing a variable to an asynchronous function (promise) in javascript

查看:102
本文介绍了传递一个变量,在JavaScript异步函数(许诺)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code这循环运行

  VAR指数=文件名[X] .lastIndexOf(/)+ 1;
VAR currentImageName =文件名[X] .substr(指数);如果(currentImageName.indexOf(JPG)!= - 1){
reader.getFileAsBlob(文件名[X])
    .done(功能(BLOB){
        picturesFilePathArray.push({
           文件名:currentImageName,
           fileURL:blobURL(BLOB)
        });
       refreshKMZList();
    });
}

我遇到的问题是,我想有2个属性的对象保存到一个数组。此对象应具有标识符和结果。 (文件名和fileURL分别)。但是,由于该功能是异步的(通过一个承诺执行)。由当时的getFileAsBlob结束后,currentImageName已经更新在许多具有相同标识符(最后一个完成它之前处理)我的对象的最终结束。

这可能是一个非常简单的问题,但我非常新的JavaScript,还没有发现任何东西了。

我认为,解决办法可能是在变量传递给完成的功能,但我觉得这个功能是通过该方法返回一个与已设置。 (我不知道它的外观)

编辑:

在code是只是一个正常的循环中。

 为(X = 0; X< fileNames.length; X ++)


解决方案

因此​​,创建一个功能,使该变量不能更改

 函数的GetFile(filmName,imageName){
    reader.getFileAsBlob(文件名)
    .done(功能(BLOB){
        picturesFilePathArray.push({
           文件名:imageName,
           fileURL:blobURL(BLOB)
        });
       refreshKMZList();
    });
}

和调用它

 如果(currentImageName.indexOf(JPG)!= -1){
    的GetFile(文件名[X],currentImageName);
}

或者你可以做这样的事情。

 如果(currentImageName.indexOf(JPG)!= -1){
    (函数(文件名,imageName){
    reader.getFileAsBlob(文件名)
        .done(功能(BLOB){
            picturesFilePathArray.push({
               文件名:imageName,
               fileURL:blobURL(BLOB)
            });
           refreshKMZList();
        });
    })(文件名[X],currentImageName);
}

MDN关闭

I have the following code which runs in a loop:

var index = fileNames[x].lastIndexOf("/") + 1;
var currentImageName = fileNames[x].substr(index);

if (currentImageName.indexOf(".jpg") != -1) {
reader.getFileAsBlob(fileNames[x])
    .done(function(blob) {
        picturesFilePathArray.push({
           fileName: currentImageName,
           fileURL: blobURL(blob)
        });
       refreshKMZList();
    });
}

The problem I am having is that I am trying to save an object with 2 properties into an array. This object should have the identifier and the result. (fileName and fileURL respectively). But since this function is asynchronous (executed through a promise). By the time the "getFileAsBlob" finishes, currentImageName has already been updated ultimately ending in many of my objects having the same identifier (the last one processed before it finished).

This might be a really easy problem, but I am very new to javascript and haven't yet found anything about it.

I thought that the solution might be in passing the variable to the "done" function, but I think this function is the one being returned by the method and is already set. (I dont know how it looks)

Edit:

The code is just inside a normal loop

for (x = 0; x<fileNames.length; x++)

解决方案

So create a function so the variable can not be changed

function getFile (filmName, imageName) {
    reader.getFileAsBlob(fileName)
    .done(function(blob) {
        picturesFilePathArray.push({
           fileName: imageName,
           fileURL: blobURL(blob)
        });
       refreshKMZList();
    });
}

and call it

if (currentImageName.indexOf(".jpg") != -1) {
    getFile (fileNames[x], currentImageName);
}

or you can do something like

if (currentImageName.indexOf(".jpg") != -1) {
    (function (fileName, imageName) { 
    reader.getFileAsBlob(fileName)
        .done(function(blob) {
            picturesFilePathArray.push({
               fileName: imageName,
               fileURL: blobURL(blob)
            });
           refreshKMZList();
        });
    })(fileNames[x], currentImageName);
}

MDN Closure

这篇关于传递一个变量,在JavaScript异步函数(许诺)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆