JavaScript How to get value of variable assigned inside Promise onSuccess outside promise

北慕城南 提交于 2019-12-20 04:35:08

问题


I'm whiting firefox extension. I got function that's reading contents of file:

var HelloWorld = {...
getData: function () {
        var env = Components.classes["@mozilla.org/processenvironment;1"].getService(Components.interfaces.nsIEnvironment);
        var path = env.get("TEMP");
        path = path + "\\lastcall.txt"
        alert(path);
        Components.utils.import("resource://gre/modules/osfile.jsm");
        let decoder = new TextDecoder(); 
        let promise = OS.File.read(path); 
        var line = null;
        promise = promise.then(
            function onSuccess(array) {
            line = decoder.decode(array)
            alert(line);
            return line;       
            }
        );
        alert("ducky:"+line+"duck");
    },
...};

I except that line will be the same because is declared outside function. From inner alert I got proper value but from outer one I get duckynullduck. how to fix it


回答1:


how to fix it

Don't use the outer alert.

That's how asynchronous code works, you can only access the data in the callbacks that are executed later. However, with promise chaining, it doesn't need to go all in the same callback or in nested callbacks.

let decoder = new TextDecoder(); 
let promise = OS.File.read(path); 
return promise.then(function onSuccess(array) {
    var line = decoder.decode(array);
    alert(line);
    return line;       
}).then(function onSuccess2(line) {
    alert("ducky:"+line+"duck");
    return line;
}); // return the promise for the line!



回答2:


getData: function () {
        var env = Components.classes["@mozilla.org/processenvironment;1"].getService(Components.interfaces.nsIEnvironment);
        var path = env.get("TEMP");
        path = path + "\\lastcall.txt"
        alert(path);
        Components.utils.import("resource://gre/modules/osfile.jsm");
        return OS.File.read(path).then(function(array) {
          let decoder = new TextDecoder(); 
          return decoder.decode(array);
        });
    },
...};

Instead of returning line you return promise for line, then the caller can do:

var line = getData();

// When you finally need the actual line, unwrap it:

line.then(function(actualLine) {

});


来源:https://stackoverflow.com/questions/24164932/javascript-how-to-get-value-of-variable-assigned-inside-promise-onsuccess-outsid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!