Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc: how to use async functions #3834

Closed
bajtos opened this issue Mar 20, 2018 · 5 comments
Closed

Doc: how to use async functions #3834

bajtos opened this issue Mar 20, 2018 · 5 comments

Comments

@bajtos
Copy link
Member

bajtos commented Mar 20, 2018

We should enhance our documentation to show how to use async functions in the following places:

  • as a custom remote method
  • as a remoting hook (beforeRemote, afterRemote, afterRemoteError)
  • as an operation hook

Related issues:

@marvinirwin
Copy link

  • Is it sufficient to just put the promise versions of each function next to the callback one?
  • Should each example be followed by an async version or just the first of each page?
  • Should there be a warning against using the callback in each example or just the first example on the page?

Old

module.exports = function(Person){

    Person.greet = function(msg, cb) {
      cb(null, 'Greetings... ' + msg);
    }

    Person.remoteMethod('greet', {
          accepts: {arg: 'msg', type: 'string'},
          returns: {arg: 'greeting', type: 'string'}
    });
};

New

module.exports = function(Person){

    Person.greet = function(msg, cb) {
      cb(null, 'Greetings... ' + msg);
    }

    // Remote methods can return promises as well, 
    // but you cannot use the callback parameter.

    // Incorrect 
    Person.greet = async function(msg, cb) {
        cb(null, 'Greetings... ' + msg);
    }

    // Correct
    Person.greet = async function(msg) {
        return 'Greetings... ' + msg;
    }

    // Correct
    Person.greet = function(msg) {
        return new Promise((resolve, reject) => {
            resolve('Greetings... ' + msg);
        });
    }

    Person.remoteMethod('greet', {
          accepts: {arg: 'msg', type: 'string'},
          returns: {arg: 'greeting', type: 'string'}
    });
};

@bajtos
Copy link
Member Author

bajtos commented May 17, 2018

Is it sufficient to just put the promise versions of each function next to the callback one?

I think it's a good start! We can always refine the first version later, based on user feedback.

Should each example be followed by an async version or just the first of each page?

I think it's enough to show async function variant only in the first example on the page. Maybe create a new sub-section in each page that's dedicated to async function? It will make it easy to create URLs pointing exactly to the example using an async function.

Should there be a warning against using the callback in each example or just the first example on the page?

I think having a warning for each example would be excessive. My proposal:

  • in the first callback-based example, add a link to the example showing an async function
  • in the section about async functions, warn users to not call the callback

@prancing-pony @strongloop/lb-next-dev Thoughts?

@marvinirwin
Copy link

Using async/await

Remote methods can also return a promise instead of using the callback parameter.

{% include code-caption.html content="/common/models/person.js" %}

module.exports = function(Person){

    Person.greet = async function(msg) {
        return 'Greetings...';
    }

    Person.remoteMethod('greet', {
          accepts: {arg: 'msg', type: 'string'},
          returns: {arg: 'greeting', type: 'string'}
    });
};

Using async/await

Operation hooks can also return a promise instead of calling the next parameter.

{% include code-caption.html content="/common/models/MyModel.js" %}

MyModel.observe('before save', async function(ctx) {
  //...
  return
});

Using async/await

Remote hooks can also return a promise instead of using the next parameter

_modelName_.beforeRemote( _methodName_, async function( ctx) {
    //...
});

This is what I propose for async examples to en/ lb3 and lb2 Remote-Methods.md, Remote-hooks and Operation-hooks.md.

@bajtos
Copy link
Member Author

bajtos commented May 21, 2018

The proposal looks reasonable. Could you please open a pull request where we can iron out any remaining details?

@dhmlau
Copy link
Member

dhmlau commented Jul 3, 2018

Closing it as resolved as PR loopbackio/loopback.io#688 has landed.
@marvinirwin, thanks again for your contribution.

@dhmlau dhmlau closed this as completed Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants