2013-12-12 5 views
8

मैं async.map का उपयोग करने की कोशिश कर रहा हूं लेकिन नीचे दिए गए उदाहरण में कुछ अनन्य कारण के लिए कॉलबैक को कॉल नहीं कर सकता, फ़ंक्शन डी को सरणी आर प्रदर्शित करना चाहिए लेकिन यह बस नहीं करता है। असल में ऐसा लगता है जैसे डी को कभी नहीं बुलाया गया था।async.map nodejs में कॉलबैक नहीं कॉल करेगा

मैं वास्तव में कुछ गलत कर रही होगी लेकिन समझ नहीं क्या

async = require('async'); 
a= [ 1,2,3,4,5]; 
r=new Array(); 

function f(callback){ 
    return function(e){ 
     e++; 
     callback(e);} 
} 

function c(data){ r.push(data); } 

function d(r){ console.log(r);} 

async.map(a,f(c),d); 

आपकी मदद

+0

वर एफसी = च (ग) के लिए अग्रिम धन्यवाद; // एफसी में कॉलबैक परम नहीं है, यह समस्या है – damphat

उत्तर

14
var async = require('async'); 

//This is your async worker function 
//It takes the item first and the callback second 
function addOne(number, callback) { 
    //There's no true asynchronous code here, so use process.nextTick 
    //to prove we've really got it right 
    process.nextTick(function() { 
    //the callback's first argument is an error, which must be null 
    //for success, then the value you want to yield as a result 
    callback(null, ++number); 
    }); 
} 

//The done function must take an error first 
// and the results array second 
function done(error, result) { 
    console.log("map completed. Error: ", error, " result: ", result); 
} 

async.map([1,2,3,4,5], addOne, done); 
संबंधित मुद्दे