代码之家  ›  专栏  ›  技术社区  ›  Martin AJ

如何调用带有promise的函数?

  •  0
  • Martin AJ  · 技术社区  · 6 年前

    var gblink = require('./getbloglinks');
    
    new Promise(function (resolve, reject) {
         var getLinks = gblink.getBlogLinks("www.example.com");
         resolve(getLinks);
    
    }).then(function (data) {
         console.log("here");
         console.log(data);
         return false;
    })
    

    gblink.getBlogLinks() 是一个函数,它获取一个URL并返回该页中的所有链接 .当我运行代码时,立即 console.log("here"); console.log(data); 将打印为 undefined

    getBlogLinks() 返回?

    gblink.getBloglinks()。


    这是

    const NN = require('nightmare');
    
    exports.getBlogLinks = function (data){
    
        const n = NN({show:true});
    
        n.goto(data)
    
        .evaluate(() => {
    
            var data = document.querySelectorAll("a[target='_blank']");
    
            arr = [];
            i=0;
            Array.from(data).forEach( function(x){
                arr[i] = x.href;
                i++;
            });
            return arr;
        })
        .then((data) => {
            return n.end(data);
    
    
        })
    }
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Sandeep Gupta    6 年前

    getBlogLinks

    const NN = require('nightmare');
    
    exports.getBlogLinks = function (data){
    
        const n = NN({show:true});
    
        return n.goto(data)
    
            .evaluate(() => {
    
                var data = document.querySelectorAll("a[target='_blank']");
    
                arr = [];
                i=0;
                Array.from(data).forEach( function(x){
                    arr[i] = x.href;
                    i++;
                });
                return arr;
            })
            .then((data) => {
                n.end(data);
                return data;
            })
    };
    

    var gblink = require('./getbloglinks');
    
    new Promise(function (resolve, reject) {
         var getLinks = gblink.getBlogLinks("www.example.com");
         console.log(getLinks);//========= Here You will get Pending promise =========
         resolve(getLinks);
    
    }).then(function (data) {
         console.log("here");
         console.log(data);//========= Here You will get the array=========
         return false;
    })
    

    编辑2:

    var gblink = require('./getbloglinks');
    
    /*
    new Promise(function (resolve, reject) {
        var getLinks = gblink.getBlogLinks("www.example.com");
        console.log(getLinks);//========= Here You will get Pending promise =========
        resolve(getLinks);
    
    })*/
    //Below is recommended way to chain the promise, avoid promise constructor, if not needed
    gblink.getBlogLinks("www.example.com")
        .then(function (data) {
            console.log("here");
            console.log(data);//========= Here You will get the array=========
            return false;
        })
    
        2
  •  0
  •   Aseem Upadhyay    6 年前

    getBlogLinks() 是在您的开发范围内实现的功能。

    getBlogLInks() 只有在有响应时才会返回响应。但你称之为同步。

    getBlogLinks 需要有一个承诺。

    getBlogLinks(data) {
      return new Promise( function(resolve,reject) {
          ....all your function code
          .then(data) {
              resolve(data); 
           }
      });
      }
    

    getBlogLinks().then