簡體   English   中英

如何在Node.js中的另一個文件中獲取嵌套的回調結果?

[英]How do I get the nested callback results inside another file in nodejs?

所以我的API代碼在此文件api.js中

api.js

var express = require('express');
var Vimeo = require('vimeo').Vimeo;
var lib = new Vimeo('dfdfdfdfdfdfd', 'WDIt+kEVudfghjklkjhgfdfghjkjhgfMaG9X3Ml', 'jhjhjhjhjhjhgfg');

var app = express();

app.listen(9999);
var results =
    app.get('/api', function (req, res) {
        lib.request(/*options*/{
            // This is the path for the videos contained within the staff picks channels
            path: '/channels/staffpicks/videos',
            // This adds the parameters to request page two, and 10 items per page
            query: {
                page: 1,
                per_page: 2,
                fields: 'uri,name,description,duration,created_time,modified_time'
            }
        }, /*callback*/function (error, body, status_code, headers) {
            if (error) {
                // console.log('error -------------------------------------');
                // console.log(error);
            } else {
                console.log(body); //how to get this in server.js
                res.send(JSON.stringify(body['data'])); //how to get this in server.js

            }
        });

    });
console.log("Server is listening");
module.exports = {
    results: results,
}

server.js

var api = require('./vimeo/api.js');
var a  =  api.results;
//blah blah
 console.log(a);// how to get calback data from api.js function here?
 }));

如何訪問api.js中可用的server.js中的console.log值?

將您的app.get()調用包裝在Promise如下所示:

var results =
  new Promise(resolve => app.get('/api', function (req, res) {
    ...
  }));

然后替換console.log(body); 與:

resolve(body);

現在results是一個Promise因此一旦解決,它將在server.js可用:

var api = require('./vimeo/api.js');
api.results.then((a) => {
  console.log(a);
});

假設api.jsserver.js都在同一進程上運行,則可以通過將其包裝在可以調用鈎子函數的函數中來向server.js添加一個函數,以攔截正在寫入process.stdout的所有內容process.stdout函數需要返回寫入process.stdout的原始參數,以便不破壞console.log()

我如何使用它的一個示例是攔截所有記錄到控制台的內容並將其寫入文件。 它不需要您要截獲console.log()任何文件中的任何其他代碼,它可以在同一進程上運行的所有寫入process.stdout工作中工作。

function hookStdOut(hook => {
  process.stdout.write = (write => {
    return (...args) => {
      args[0] = hook[args[0]]
      write.apply(process.stdout, args)
    }
  })(process.stdout.write)
}

hookStdOut(chunk => {
  // access whatever was being written to stdout and process
  // you cannot use console.log within this function 
  // because it will result in an infinite loop since 
  //console.log writes to process.stdout

  // return the chunk so it will be written back to process.stdout
  return chunk
})

您可以在server.js中導出一個函數,並在可用時立即傳遞api.js中的異步數據。

server.js

var api = require('./vimeo/api.js');
module.exports = function(data){
  var a  =  api.results;
 console.log(data); //your async data

}

並在api.js中

//after you receive the data
else {
  var serverModule = require('./server');
  serverModule(data); //call the method
  res.send(JSON.stringify(body['data']));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM