Bifalgorithm post

Every function in JavaScript has to return something, even if it's undefined. Like this get the other function, it returns sunny. But sometimes your function doesn't have the thing that it needs to return. Like if it has to call an API to get the weather first. So you can't do this. Why? Because call API will actually take time. It's asynchronous. So this function actually doesn't return the weather. It doesn't work. The good news is it can return the promise of the weather instead, like this. It says hold my beer while I go get the weather. There are two parts to any promise. The promise creator, like our function and the promise receiver, which is whoever calls our function. What's cool about promises is that the receiver can say, Oh, I see you need some time to go get the data. When you're ready, call this function and I'm putting in the then block and send me the data through that. But also if you run into any issues, call this other function that I'm putting in the catch block. But so how does the creator call those functions? Remember the promised object? It takes a function with two parameters. You guessed it, then and catch, except we call them resolve and reject. Now you can do your async operation and call resolve or reject when you ready. Cool stuff. There's a lot more to promises that can jam into one minute. Watch the full episode below.

Every function in JavaScript has to return something, even if it's undefined. Like this get the other function, it returns sunny. But sometimes your function d…

Ideas in this flowchart

  • JavaScript Function Return
  • Function Return Requirement
  • Return Something
  • Undefined Return
  • API Call Issue
  • Asynchronous Nature
  • Promise Solution
  • Promise of Weather
  • Promise Parts
  • Promise Creator
  • Promise Receiver
  • Receiver's Role
  • Then Block
  • Catch Block
  • Promise Object
  • Resolve and Reject
  • Async Operation
Feed/

Every function in JavaScript has to return something, even if it's undefined. Like this get the other function, it returns sunny. But sometimes your function doesn't have the thing that it needs to return. Like if it has to call an API to get the weather first. So you can't do this. Why? Because call API will actually take time. It's asynchronous. So this function actually doesn't return the weather. It doesn't work. The good news is it can return the promise of the weather instead, like this. It says hold my beer while I go get the weather. There are two parts to any promise. The promise creator, like our function and the promise receiver, which is whoever calls our function. What's cool about promises is that the receiver can say, Oh, I see you need some time to go get the data. When you're ready, call this function and I'm putting in the then block and send me the data through that. But also if you run into any issues, call this other function that I'm putting in the catch block. But so how does the creator call those functions? Remember the promised object? It takes a function with two parameters. You guessed it, then and catch, except we call them resolve and reject. Now you can do your async operation and call resolve or reject when you ready. Cool stuff. There's a lot more to promises that can jam into one minute. Watch the full episode below.

"Every function in JavaScript has to return something"

👍
0
0 comments0 shares

Sign in to view and post comments.