Serverless-offline: Babel runtime requires return Promise?

Created on 31 May 2016  路  1Comment  路  Source: dherault/serverless-offline

Hi guys, thanks again a great plugin, this is compulsory for development using Gateway & Lambda. Currently I'm having a function with babel runtime. However, I always get the following error when using context.done. This is really a show-stopper.
Serverless: Warning: context.done called twice within handler 'user_bookmark_create'!

My function is very simple though:

'use strict';

export default (event, context) => {
    setTimeout(function(){
        context.done(null, 'DONE');
    }, 2000);
};

It looks like we require a Promise to be returned from handler here:
From src\index.js:

// Promise support
                if (funRuntime === 'babel' && !this.requests[requestId].done) {
                  if (x && typeof x.then === 'function' && typeof x.catch === 'function') {
                    x.then(lambdaContext.succeed).catch(lambdaContext.fail);
                  }
                  else if (x instanceof Error) lambdaContext.fail(x);
                  else lambdaContext.succeed(x);
                }

The handler above is working perfectly with Lamdba.

So my questions is why do we require returning a promise if we use babel runtime?.

Looking at the code here:

// Promise support
                if (funRuntime === 'babel' && !this.requests[requestId].done) {
                  if (x && typeof x.then === 'function' && typeof x.catch === 'function') {
                    x.then(lambdaContext.succeed).catch(lambdaContext.fail);
                  }
                  else if (x instanceof Error) lambdaContext.fail(x);
                  else lambdaContext.succeed(x);
                }

Looks like the last else is causing problem, I think we shouldn't call lambdaContext.succeed(x); as the handler might be still working and going to execute context.done as my code above.

Thanks again!!! ( I'm more than happy to PR to fix this. )

Most helpful comment

Hi @tuanmh, you make a good point. I'll merge your PR, thanks for it. But this might break other people's code and open more issues ;) :monkey:

>All comments

Hi @tuanmh, you make a good point. I'll merge your PR, thanks for it. But this might break other people's code and open more issues ;) :monkey:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Looveh picture Looveh  路  4Comments

stunningpixels picture stunningpixels  路  3Comments

conradoramalho picture conradoramalho  路  3Comments

aldofunes picture aldofunes  路  3Comments

ktwbc picture ktwbc  路  4Comments