0
   

How to deal by passing callback inside asynchronous function ?

 
 
Reply Sun 16 Jul, 2017 01:26 pm

The async function itself should make use of the lookup() function but return the result inside the callback.

The parameters for the callback are err and res.

If an Error has been thrown by lookup() then it should be passed to err, otherwise err is null or undefined.If a result has been returned by lookup() then it should be passed to res, otherwise res is null or undefined.


<!DOCTYPE html>
<html>
<body>
<script src="https://wzrd.in/standalone/[email protected]"></script>
<script src="https://wzrd.in/standalone/[email protected]">
</script>
<script src="https://wzrd.in/standalone/[email protected]"></script>
<script>
window.test = tape;
tapBrowserColor();
</script>
</body>
</html>

<script>

const users = [
{
"login": "knuth",
"firstName": "Donald",
"lastName": "Knuth",
"likes": ["C", "Unix"]
},
{
"login": "norvig",
"firstName": "Peter",
"lastName": "Norvig",
"likes": ["AI", "Search", "NASA", "Mars"]
},
{
"login": "mfowler",
"firstName": "Martin",
"lastName": "Fowler",
"likes": ["Design Patterns", "Refactoring"]
},
{
"login": "kent",
"firstName": "Kent",
"lastName": "Beck",
"likes": ["TDD", "wikis", "Design Patterns"]
}
];

// lookup()
const lookup = (login, prop) => {
// Only change code below this line
const found = users.find(function(e){
return e.login === login;
});
if(!found) {
throw "Could not find user";
} else {
if(prop in found) {
return found[prop];
} else {
throw "Could not find property";
}
}
};

// lookupAsync()
const lookupAsync = (login, prop, callback) => {
// Only change code below this line
const found = users.find(function(e){
return e.login === login;
});
if(!found) {
throw "Could not find user";
} else {
if(prop in found) {
return found[prop];
} else {
throw "Could not find property";
}
}

//my intension below but doesn't work
//callback();
//lookupAsync(function (err,res) {});

};
test('lookupAsync() likes', assert => {
const msg = `lookupAsync(<login>, 'likes', callback) should return likes for the specified user.`;
lookupAsync('norvig', 'likes', function(err, res){
const actual = res;
const expected = ["AI", "Search", "NASA", "Mars"];
assert.deepEqual(actual, expected, msg);
assert.end();
});

});


test('lookupAsync() last name', assert => {
const msg = `lookupAsync(<login>, 'lastName') should return the last name for the specified user.`;

lookupAsync('knuth', 'lastName', function(err, res){
const expected = 'Knuth';
const actual = res;
assert.deepEqual(actual, expected, msg);
assert.end();
});
});


test('lookupAsync() with unknown user', assert => {
const msg = `lookupAsync() with unknown user should return an error with the correct message.`;
const value = lookupAsync('nobody', 'likes', function(err, res){
const actual = err.message;
const expected = 'Could not find user.';
assert.equal(actual, expected, msg);
assert.end();
});
});


test('lookupAsync() with unknown property', assert => {
const msg = `lookupAsync() with unknown property should return an error with the correct message`;
lookupAsync('mfowler', 'noprop', function(err, res){
const actual = err.message;
const expected = 'Could not find property.';
assert.equal(actual, expected, msg);
assert.end();
});
});


</script>

The output should look like(short from) :

1..4
# tests 4
# pass 4
# ok

  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Question • Score: 0 • Views: 278 • Replies: 0
No top replies

 
 

Related Topics

 
  1. Forums
  2. » How to deal by passing callback inside asynchronous function ?
Copyright © 2019 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 03/22/2019 at 09:03:53