Callback functions in Javascript
it is highly recommended to have completed Sir-Mix-Alot first.
Write and invoke callback functions. Practice asynchronous execution in sequence.
npm install
mocha -w --bail
do all work in lib/callbacks.js
declare a function named wait
that accepts two arguments, seconds
and callback
.
wait
will invoke the callback
function after n
seconds, where n
is the value of the seconds
argument.
log the message "wait 3 started".
invoke wait
with 3
as the first argument, and an unnamed callback function as the second argument.
the unnamed callback function logs the message "wait 3 done".
the expected behavior is that the first message will be printed out to the console immediately, then the second message will be printed out to the console after 3 seconds.
declare a function named repeat
that accepts two arguments, times
and callback
.
repeat
will invoke the callback
function n
many times, where n
is the value of the times
argument.
callback
will be invoked with one argument passed in, which is the value of the iteration step in which it was invoked. the first time callback
is invoked, 0
will be passed to it, the third time callback
is invoked, 2
will be passed to it.
invoke the repeat
function passing in 10
as the first argument, and a callback function for the second argument.
the callback function accepts one argument, iteration
.
the callback function logs the result of adding 100
to the value of iteration
.
the expected behavior is that the numbers 100 through 109 will be printed out to the console.
Perform the following sequence:
- wait 4 seconds seconds before continuing to the next step
- repeat the following steps 2 times
- log "repeating for i
[n]
" where[n]
is current iteration fromrepeat
- wait
[n] * 3
seconds before continuing to the next step, wheren
is the current iteration fromrepeat
- repeat the following steps 3 times
- log the following message
"i [n] j [m]"
where[n]
is the current iteration from step 2. and[m]
is the current iteration from step 5.
the expected behavior is that, after 3 seconds, the following will be printed out to the console
wait 3 done
repeating for i 0
repeating for i 1
i 0 j 0
i 0 j 1
i 0 j 2
then after 3 more seconds, this will be printed out to the console
i 1 j 0
i 1 j 1
i 1 j 2
- Import the module found in
./datastore.js
- Create an empty class named
User
with a constructor that takes 0 arguments. - Add a static method named
find
to theUser
class that takes 2 arguments,query
andcallback
- the
find
function will invoke thecallback
function, passing back all user objects from the datastore, where every key found in thequery
argument, matches the value of the matching key in the user object from the datastore. - if
query
has a key that does not exist in the imaginary user schema, invoke thecallback
function passing back a javascirptRangeError
with a very descriptive error message. - if
query
has a key that does exist in the imaginary user schema, however the values of thequery
param and theuser
param are of different types, invoke thecallback
function passing back a javascirptTypeError
with a very descriptive error message.
The callback
function signature is function(error, users)
where error
is an Error
object if there are any errors, else is null
and users
is an array containing any users found by the query.
Invoke the User.find
method with
query
: { id : 2 }
callback
: function with two parameters, error
and users
if the error is not null, throw
the error
log a message with 'users'
and the users
result
the expected result of the callback function is
users [ { id: 2, name: 'goldstine', mood: 'reverent' } ]
Invoke the User.find
method with
query
: { happy : true, iKnowIt : true }
callback
: function with two parameters, error
and users
if the error is not null, log the error type, ":", and the error message
log a message with 'users'
and the users
result
the expected result of the callback function is
RangeError : query argument `iKnowIt` is not a property of "User"
users []
Invoke the User.find
method with
query
: { id : 4, name : "davinci" }
callback
: function with two parameters, error
and users
if the error is not null, throw
the error
log a message with 'users'
and the users
result
the expected result of the callback function is
users [ { id: 4, name: 'davinci', mood: 'elated' } ]
Invoke the User.find
method with
query
: { mood : "elated" }
callback
: function with two parameters, error
and users
if the error is not null, throw
the error
log a message with 'users'
and the users
result
the expected result of the callback function is
users [ { id: 3, name: 'curie', mood: 'elated' },
{ id: 4, name: 'davinci', mood: 'elated' },
{ id: 5, name: 'mccarthy', mood: 'elated' } ]
Invoke the User.find
method with
query
: { id : "two" }
callback
: function with two parameters, error
and users
if the error is not null, log the error type, ":", and the error message
log a message with 'users'
and the users
result
the expected result of the callback function is
TypeError : query arguments are incorrect types, `id` is "string", expected "number"
users []