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

后端 未结 2 1095
误落风尘
误落风尘 2021-01-24 19:40

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

var HelloWorld = {...
getData: function () {
        var env = Components.classe         


        
相关标签:
2条回答
  • 2021-01-24 20:21
    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) {
    
    });
    
    0 讨论(0)
  • 2021-01-24 20:24

    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!
    
    0 讨论(0)
提交回复
热议问题