Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

async
javascript

How to use log(function) in Async

Nouman Abbasi

In the Async module, log(function) logs the result of an async function to the console.

If multiple arguments are returned from the async function, console.log is called on each argument in order.

log(function) only works in Node.js or in browsers that support console.log and console.error.

Syntax

async.log(function, arguments...)

  • function: An AsyncFunction that you eventually want to apply all arguments to.
  • arguments...: Any number of arguments to apply to the function.

Example

In this example, we are creating a simple async function (called hello) that takes the argument name and a callback function.
We then use log(hello, 'world') to log the results of the hello function with the argument name='world'.

import log from 'async/log';

// A simple AsyncFunction that prints
// Hello after a certain timeout
var hello = function(name, callback) {
    setTimeout(function() {
        callback(null, 'hello ' + name);
    }, 1000);
};

// Using async.log to log the result of
// hello function to the console
log(hello, 'world');

RELATED TAGS

async
javascript

CONTRIBUTOR

Nouman Abbasi
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring