Q.js - Using deferred

后端 未结 5 1017
南笙
南笙 2021-02-07 11:36

How do I get the value of the text from the example below?

Q.js has an example on using Deferred:

var deferred = Q.defer();
FS.readFile(\"foo.txt\", \"ut         


        
5条回答
  •  你的背包
    2021-02-07 12:29

    Q = require('q');
    FS = require('fs');
    
    function qread() {
      var deferred = Q.defer();
      FS.readFile("foo.txt", "utf-8", function (error, text) {
        if (error) {
      deferred.reject(new Error(error));
        } else {
      deferred.resolve(text);
        }
      });
      return deferred.promise;
    };   
    
    var foo = qread();
    
    setTimeout(function() {
      console.log(""+foo);
    },1000);
    

    It's strange you cannot see the output for console.log(foo). Dont' know why.

    Check more examples here https://github.com/kriskowal/q/wiki/Examples-Gallery

提交回复
热议问题