在iteratee函数完成之前调用异步回调

时间:2016-11-13 17:18:06

标签: javascript node.js asynchronous google-calendar-api

我使用异步库(http://caolan.github.io/async/docs.html#map)来处理节点应用程序中的一些异步调用。我的问题是,当我使用async的map方法时,在所有iteratee函数完成之前调用方法的回调。谁知道为什么?

这里是调用地图的代码:

function divvy(email, project, oauth2Client, callback) {
  getUserPreferences(email, (preferences) => {
    calendars.getCalendars(oauth2Client, (calendars) => {
      var calendarIds = calendars.map(function(obj) { return obj.id })
      console.log('length of calendarIds is: '+calendarIds.length)
      asyncMap(calendarIds, events.getEventsUpTo.bind(null, project.end, oauth2Client), function(err, results) {
        // results is now an array of arrays. 
        // Each array within results is an array of events per calendarList
        console.log('final callback is called')
        callback(true)
      });
    })
  })
}

这里是为calendarIds中的每个项目调用的iteratee代码:

function getEventsUpTo (projectEnd, oauth2Client, calendarId, callback) {
  calendar.events.list({
    auth: oauth2Client,
    calendarId: calendarId,
    timeMin: (new Date()).toISOString(),
    timeMax: projectEnd.toISOString(),
    maxResults: 10,
    singleEvents: true,
    orderBy: 'startTime',
  }, (err, response) => {
    if (err) {
      // console.error('The API returned: ' + err)
      console.log('reach callback call within iteratee function')
      callback(null, null)
    } else {
      var events = response.items
      if (events.length === 0) {
        callback('No upcoming events')
      } else {
        console.log('reach callback call within iteratee function')
        callback(null, events)
      }
    }
  })
}

最后,这是控制台的输出:

length of calendarIds is: 6
reach callback call within iteratee function
reach callback call within iteratee function
reach callback call within iteratee function
final callback is called
reach callback call within iteratee function
reach callback call within iteratee function

一些上下文:我使用地图从Google Calendar的API中检索日历事件列表。

谢谢!

1 个答案:

答案 0 :(得分:0)

注册的所有回调时,返回true。你需要回调来执行你的回调:

call(1);
registercallback(()=>{call()});

在您的异步功能上执行:

 app.config(['$provide', '$httpProvider', function ($provide, $httpProvider) {
    $provide.decorator('$interceptorManager', ['$delegate', '$injector', function ($delegate, $injector) {
        $delegate.addInterceptor = function (loginOptionValue) {                            
            if (loginOptionValue) {
                switch (loginOptionValue) {
                    case 'Auth 0':
                        var index = $httpProvider.interceptors.indexOf('ProtectedResourceInterceptor');
                        if (index != -1) $httpProvider.interceptors.splice(index, 1);
                        $httpProvider.interceptors.push('jwtInterceptor');
                        break;
                    case 'Azure Ad':
                        var index = $httpProvider.interceptors.indexOf('jwtInterceptor');
                        if (index != -1) $httpProvider.interceptors.splice(index, 1);
                        $httpProvider.interceptors.push('ProtectedResourceInterceptor');
                        break;
                }
            }
            console.log('Adding Interceptio: '+$httpProvider.interceptors.length);
        }
        $delegate.removeInterceptors = function (loginOptionValue) {
            var index = -1;
           //var index = $httpProvider.interceptors.indexOf('ProtectedResourceInterceptor');
           //if (index != -1) $httpProvider.interceptors.splice(index, 1);

           index = $httpProvider.interceptors.indexOf('jwtInterceptor');
           if (index != -1) $httpProvider.interceptors.splice(index, 1);
        }
        $delegate.printInterceptors = function () {
            for (var i = 0 ; i < $httpProvider.interceptors.length ; i++)
                console.log($httpProvider.interceptors[i]);
        }
        return $delegate;
    }]);
}]);
相关问题