MOCKSTACKS
EN
Questions And Answers

More Tutorials









NodeJS Creating a Nodejs Library that Supports Both Promises and ErrorFirst Callbacks

Example Module and Corresponding Program using Bluebird



math.js


'use strict';
const Promise = require('bluebird');
module.exports = {
 // example of a callback-only method
 callbackSum: function(a, b, callback) {
 if (typeof a !== 'number')
 return callback(new Error('"a" must be a number'));
 if (typeof b !== 'number')
 return callback(new Error('"b" must be a number'));
 return callback(null, a + b);
 },
 // example of a promise-only method
 promiseSum: function(a, b) {
 return new Promise(function(resolve, reject) {
 if (typeof a !== 'number')
 return reject(new Error('"a" must be a number'));
 if (typeof b !== 'number')
 return reject(new Error('"b" must be a number'));
 resolve(a + b);
 });
 },
 // a method that can be used as a promise or with callbacks
 sum: function(a, b, callback) {
 return new Promise(function(resolve, reject) {
 if (typeof a !== 'number')
 return reject(new Error('"a" must be a number'));
 if (typeof b !== 'number')
 return reject(new Error('"b" must be a number'));
 resolve(a + b);
 }).asCallback(callback);
 },
};

index.js


'use strict'
;
const math
= require
('./math'
)
;
// classic callbacks
math.callbackSum
(
1
,
3
, function
(err
, result
)
{
 if
(err
)
 console.log
('Test 1: '
+ err
)
;
 else
 console.log
('Test 1: the answer is '
+ result
)
;
}
)
;
math.callbackSum
(
1
, 'd'
, function
(err
, result
)
{
 if
(err
)
 console.log
('Test 2: '
+ err
)
;
 else
 console.log
('Test 2: the answer is '
+ result
)
;
}
)
;
// promises
math.promiseSum
(
2
,
5
)
.then
(function
(result
)
{
 console.log
('Test 3: the answer is '
+ result
)
;
}
)
.catch
(function
(err
)
{
 console.log
('Test 3: '
+ err
)
;
}
)
;
math.promiseSum
(
1
)
.then
(function
(result
)
{
 console.log
('Test 4: the answer is '
+ result
)
;
}
)
.catch
(function
(err
)
{
 console.log
('Test 4: '
+ err
)
;
}
)
;
// promise/callback method used like a promise
math.sum
(
8
,
2
)
.then
(function
(result
)
{
 console.log
('Test 5: the answer is '
+ result
)
;
}
)
.catch
(function
(err
)
{
 console.log
('Test 5: '
+ err
)
;
}
)
;
// promise/callback method used with callbacks
math.sum
(
7
, 11
, function
(err
, result
)
{
 if
(err
)
 console.log
('Test 6: '
+ err
)
;
 else
 console.log
('Test 6: the answer is '
+ result
)
;
}
)
;
// promise/callback method used like a promise with async/await syntax
(async () => {
 try {
 let x = await math.sum(6, 3);
 console.log('Test 7a: ' + x);
 let y = await math.sum(4, 's');
 console.log('Test 7b: ' + y);
 } catch(err) {
 console.log(err.message);
 }
})();


Conclusion

In this page (written and validated by ) you learned about NodeJS Creating a Nodejs Library that Supports Both Promises and ErrorFirst Callbacks . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS Project Structure.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.