Midwest Dev Chat

MidwestDevChat.com

  • The newest 15 messages in the super-cool #javascript channel.

  • 07/19 14:43:22 Morris: clap
  • 07/19 14:46:17 Charity: to bubble that station up, you need to return the whole promise chain from your outer function:
      return callApi().then(function(station) {
        return station
      })
    }```
    but you may notice that if all you're doing is returning the value, you might as well
    ```async function getABunchOfThings() {
      return callApi()
    }```
    but you've got four of them, and this won't work:
    ```async function getABunchOfThings() {
      return callApi1()
      return callApi2()
    }
    because callApi2 will never be executed
  • 07/19 14:46:34 Charity: That's where something like Promise.race() or Promise.all() will come in.
  • 07/19 15:20:11 Luise: @Morris I didn't read everything written above, but based on @Charity trying to explain to me his understanding of your question, I wonder if you might be interested in https://github.com/sindresorhus/p-locate
  • 07/19 15:53:53 Morris: Ok sorry guys, kind of a shitty question asker hence the reason I usually google for hours lol... so here goes: file1.js -
    function getStreamStation(urlParam, callback) {
      return new Promise(function(resolve, reject) {
    ... blah blah..
    ...
    client.on("data", x => {
          dataCallback(x)
            .then(returningStation => {
    // NEEDED: pass value of returingStation into the resolve for the promise...
    // logging returningStations gives correct value
    // resolving any random thing gives correct value in file2.js
              resolve(returningStation);
            })
            .catch(err => {
              errorCallback;
            });
        });
    
    async function dataCallback(response) {
          var resultingStation = null;
          var title = null;
          const responseString = response.toString();
    
          // Append to the buffer and check if our title is fully included yet
          // We're looking for a string with the format of
          // StreamTitle=Artist Name - Song Name;
          buffer += responseString;
    
          var titlecheck = getDetailsFromBuffer(buffer);
          if (titlecheck != null) {
            resultingStation = await handleBuffer(buffer);
            tearDown();
            return resultingStation;
          }
    
          if (buffer.length > maxBufferSize) {
            return returnError();
          }
        }
    
    ... end of file1.js
    ```
    
    file2.js-
    ```
    ... blah blah..
    ...
     async function findStation(url, asyncCallback) {
        try {
    ...
    ...
    // where icystream.getStreamStation is the function from file1.js
    const ICYSTREAM = icystream.getStreamStation(url, function(
            error,
            station
          ) {
            asyncResultReturned(error, station, asyncCallback);
          });
    ..
    ...
    ICYSTREAM.then(x => {
    // resolving any random thing in file1.js gives correct output, just not the returningStation variable
            console.log(x);
            if (x != null) {
              return x;
            }
          });
    
    Problem is im a getting undefined in file2.js log. If log it at file1 in the then block it shows the value of the correct variable, and if i resolve anything it will show up in file1 then block however I cant get the value for returningStation from file1.js to the then block of file2.js
  • 07/19 16:24:52 Charity: `new Promise(function(resolve, reject)` This is something you shouldn't have to use in the course of normal events. Usually `new Promise` is what you need to adapt a callback-based API to use promises. However, now that there's https://nodejs.org/api/util.html#util_util_prom..., I rarely use `new Promise`. `dataCallback(x).then()` What is dataCallback? Does it return a thing that has a `then()` on it? `.then(returningStation => resolve(…)` This probably isn't doing what you expect it to do. I recommend writing it so that the inner thing can't force the outer thing to resolve. Instead, you could use `await` to get a station, then: • If you want `getStreamStation` to take a callback, `callback(theStationYouGot)` • If you want `getStreamStation` to return a promise, `return theStationYouGot`
  • 07/19 16:32:01 Morris: I edited file1.js to include dataCallback, sorry! Its crazy that I can put resolve in the then block and it handles it appropriately but when i use the response it wants nothing to do with it lol! I tried resolving the on event but it just passes the function to the parent.
  • 07/19 16:33:31 Charity: `resolve` is coming from the Promise constructor `new Promise((resolve, reject)`. No matter how deep you are, when you call it, it will cause the promise from `new Promise` to resolve. This is potentially confusing if there's more than one place that calls `resolve`, or if `resolve` never gets called. My personal preference is to use `promisify` to sidestep the whole problem
  • 07/19 16:33:39 Morris: I also tried assigning a variable to the local scope but it wont change the value inside the on data function so when i resolve it, the variable remains unchanged ugh
  • 07/19 16:34:07 Morris: ya there is only one resolve!
  • 07/19 16:35:09 Charity: I've got to get going, but hit this channel up later if you still have questions?
  • 07/19 16:35:19 Morris: ok thanks you so much for your time!!!
  • 07/19 19:08:29 Morris: >>>resolve is coming from the Promise constructor new Promise((resolve, reject). No matter how deep you are, when you call it, it will cause the promise from new Promise to resolve
  • 07/19 19:19:13 Morris: Right, I get that that it's coming from the constructor and that its required for the promise to work. Otherwise it pops a error. What I'm not 100% sure on is the scope of things. JS scope is a wee bit confusing if your used to server side lang. So if return the promise from the function, anywhere inside the promise the resolve acts like a return statement, right? So we have this rather long function but when it hits resolve it should exit out, correct? Or am I way off on that? I would prefer not to use a library for a native function, ya know dependencies can get out of control in a hurry!
  • 07/19 20:24:32 Morris: got it hacked together! lol not my finest moment..(thinks to himself)..._wait i dont have fine moments_.... Basically i add a new property to the returning object, then the resolve will take the complete object and pass it through... I think the variable needed to be initialized in some form before resolving, otherwise JS would just resolve nothing, hence the undefined.
  • *Usernames have been changed to protect the innocent.
We're currently 1184 members strong. Join us!
Request Invite

Check out all the cool channels!

Join the conversation!