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

Plus d'informations
il y a 4 mois 2 jours - il y a 4 mois 1 jour #156861 par zesanriky
zesanriky a créé le sujet : How can I invoke the callback function from argument in Javascirpt ?
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/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
Dernière édition: il y a 4 mois 1 jour par DenisChenu. Raison: [code]

Connexion pour participer à la conversation.

Plus d'informations
il y a 4 mois 1 jour #156871 par DenisChenu
DenisChenu a répondu au sujet : How can I invoke the callback function from argument in Javascirpt ?
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 (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Connexion pour participer à la conversation.

Commencez dès maintenant !

Créez simplement un compte et commencez à utiliser LimeSurvey dès aujourd'hui.

Inscrivez-vous maintenant

Inscrivez-vous à notre Newsletter!