Skip to content

Instantly share code, notes, and snippets.

@shcyiza
Last active April 25, 2019 12:38
Show Gist options
  • Save shcyiza/d938b07781442f4c04efebf4ff399272 to your computer and use it in GitHub Desktop.
Save shcyiza/d938b07781442f4c04efebf4ff399272 to your computer and use it in GitHub Desktop.
RandomService.formatPayload = function (id, fn) {
/* Should deduce that RandomService is an object, has a property named 'formatPayload'
a funtion with two arguments is being expressed on the property, so it's a method */
return {
// this method returns an object
client_id: id,
/* returned object has a property 'client_id', which has the id argument as value or a reference
we cannot deduce the type of the 'id' argument
could be a primitive or an object */
body: fn().data
/* returned object has a property 'body'
which has the property 'data' of the result of the called fn argument as a value or a reference
We know for sure now fn is a callback that should return an object with data as a propery
but we cannot deduce the type of 'data'*/
}
}
RandomService.formatPayload("AZER1234", () => {
/* the method is now being called.
first argument is a string which is ok cause it could have any type
the secound argument is an expressed callback which is what was expected */
const result = {
/* result is being declared as an oject in the context of RandomService
cause we are using an arrow function */
status_code: 200,
data: {
name: "Mario",
profession: "plumber"
}
}
// callback ends without returning anything
})
/* this code should throw an error "Cannot read property 'data' of undefined"
Cause the callback don't return anything*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment