Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

How can I invoke the callback function from argument in Javascirpt ?

  • zesanriky
  • zesanriky's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
6 years 8 months ago - 6 years 8 months ago #156861 by zesanriky
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.
Code:
<!DOCTYPE html>
<html>
<body>
<script src="https://wzrd.in/standalone/tape@latest"></script>
<script src="https://wzrd.in/standalone/tap-browser-color@latest">   
</script>
<script src="https://wzrd.in/standalone/deep-freeze@latest"></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
Last edit: 6 years 8 months ago by DenisChenu. Reason: [code]
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 8 months ago #156871 by DenisChenu
Why is related to limesurvey ?

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose