none
Azure Function does not execute promise on NodeJS RRS feed

  • Question

  • I am trying to build several Azure Functions as part of a project. This project is built on NodeJS. The functions all need to reuse code from some models, so the project folder structure is current done in the following way:

    getToken (this is a function)

    | - function.json

    | - index.js

    | - node_modules

    | - package.json

    verifyToken (another function)

    models (code to reuse)

    | - userModel.js

    lib (code to reuse)

    | - azureStorageHelper.js

    I import the reusable code via require statements. For example, the getToken function has the following code:

    var jwt = require('jsonwebtoken'),
    	config = require('../config.js'),
    	Q = require('q'),
    	env = config.environment,
    	user_model = require('../models/userModel.js'),
    	secret = config.session.secret[env];
    
    function getUser(emailAddress){
    	var deferred = Q.defer();
    	user_model.getUserByEmail(emailAddress, function(error, result){                
    		if(error) {
    			deferred.reject(new Error(error));
    		} else {
    			context.log('return user: %j:', result);
    			deferred.resolve(result);
    		}        
    	});
    	return deferred.promise;
    }
    
    module.exports = function(context, req) {
    	var username = req.body.username.toLowerCase(),
    		password = req.body.password;
    	username = username.trim();
    
    	getUser(username)
    	.then(function(obj) {
    		context.log('received object: %s', obj);
    		context.res = {
    			status: 200,
    			body: JSON.stringify(obj)
    		};
    		context.done();	
    	});
    }

    However, the line  user_model.getUserByEmail(emailAddress, function(error, result) never returns a result at all (its callback function is never called). Running this same code in my local machine returns without a problem.

    So I am wondering if there is a limitation on Azure functions in regards to setting up a project to use shared code libraries within the same project? I am doing something wrong by trying to import a JS file that is stored in a folder at the root of the project? What is the correct structure in order to have functions reuse common code?


    • Edited by luisdel Wednesday, June 22, 2016 7:01 AM
    Wednesday, June 22, 2016 7:00 AM

All replies

  • Hey Luis,

    Your context.log statement in that function is likely failing and throwing an exception. You need to pass the context to your getUser function.

    Tuesday, June 28, 2016 7:26 PM
  • Hi Chris,

    I'll check if that line is indeed in the code running on azure, it might have been a copy/paste error here. However, if that were the case, shouldn't the exception be shown in the streaming logs in the azure portal? Afaik, this function runs and never returns anything, neither in the azure portal streaming logs window. 

    Tuesday, June 28, 2016 7:31 PM
  • Yeah, that's likely a usability issue with us. If your function throws an exception, we need to be returning that in your logics. Do you see anything when you look at your WebJobs Dashboard (via the Monitoring tab for your Function)?
    Tuesday, June 28, 2016 8:04 PM
  • Just spend a reasonable amount of time to get graphql working in Functions with no success. Decided to see if promises worked. I do not think they do....

    This code does not call context.done()

    module.exports = function(context, req) {
         var urls = [];
      // Promise.all([]) should resolve immediately with an empty array!
      // https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
      Promise.all(urls).then(() => context.done());
    };

    Friday, July 29, 2016 4:47 AM
  • looks like a bug in Edge.js https://github.com/tjanczuk/edge/issues/325
    Saturday, July 30, 2016 12:24 AM
  • This still doesn't look like it's working. Any progress on it, any place to file a request to have this fixed?

    Blog: http://radu.grama.name - Twitter: @RaduGrama

    Saturday, September 3, 2016 3:30 AM
  • We are already tracking some issues with promises in our repo, e.g. here. Feel free to chime in on that issue, or create a new issue if you think your issue is different. Thanks :)

    Mathew Charles [MSFT]


    Saturday, September 3, 2016 3:38 PM