Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

nunjucks-setasync

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nunjucks-setasync

Set Async tag

latest
Source
npmnpm
Version
1.0.4
Version published
Weekly downloads
0
-100%
Maintainers
2
Weekly downloads
 
Created
Source

nunjucks-set-async

Like set, but for async functions.

Usage

You can use this extension in the following 4 easy steps:

  • Create a new nunjucks Environment. let env = new nunjucks.Environment();
  • Add the nunjucks-set-async to the environment. env.addExtension('SetAsyncExtension', new SetAsyncExtension())
  • In your template, call the extension as follow: {% setAsync 'variable', functionName, [functionArguments] %} variable is the name of the variable that will hold the results of functionName with [functionArguments]. If the function functionName does not take any arguments, you need to still pass an empty array {% setAsync 'variable', functionName, [] %}.
  • Use {{ variable }} to render the value of variable anywhere after the setAsync block.

Example

In this example we are simulating getting the number of astronauts currently in space with an asynchronous function:

  • index.js
let getNumberOfAstronauts =  function(cb) {
  setTimeout(function() {
    cb(null, 12);
  }, 10);
}
env.addGlobal('getNumberOfAstronauts', getNumberOfAstronauts);
env.addExtension('SetAsyncExtension', new SetAsyncExtension())
  • template.html
{% setAsync 'numberofAstronauts', getNumberOfAstronauts, [] %}
<p> There are {{numberofAstronauts}} in space right now! </p>

Or you can also use setAsync with parens as follow:

{% setAsync('numberofAstronauts', getNumberOfAstronauts, []) %}
<p> There are {{numberofAstronauts}} in space right now! </p>
  • index.js
env.render('template.html', function(err, res) {
  console.log(res);
});

FAQs

Package last updated on 21 Mar 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts