Asynchronous Javascript
One important difference between Napkin's Javascript runtime v.s pure Node.js is how Promises are handled. In Node.js, you can make an async IO call without explicitly using await
and your program will
still wait for the call to return before exiting. In Napkin, you should explicitly await
any Promise object to ensure that it resolves before your function exits.
✅ Always use await
import axios from 'axios'
export default async (req, res) => {
// Using await here ensures the Promise resolves before the function exits
await axios.get('example.com/cat').then(res.json)
}
❌
import axios from 'axios'
export default async (req, res) => {
// This function will exit before the request to example.com/cats completes
axios.get('example.com/cat').then(res.json)
}