0

I created a "protocolla" function. The first time I call it, the promise is resolved. The second time I call it, however, the promise remains hanging.

I added some test variables to store the references to the "resolve" and "reject" functions and a couple of console logs that log "true" on the first execution and "false" on the second execution. I don't understand why it doesn't work the second time and I don't know how to fix it. Thank you.

let myReject, myResolve;
export const protocolla = (protocollaCommand: ProtocollaCommand): Promise<string> => {
    const operation = new Promise<string>((resolve, reject) => {
        myReject = reject
        myPromise = promise
        authOperation(token => {
            const requestOptions = {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': 'Bearer ' + token
                },
                body: JSON.stringify(protocollaCommand)
            };
            return fetch(baseUrl + "/api/protocollaMail", requestOptions);
        }, (res: Response) => {
            console.log('[JOR] reject', myReject === reject)
            console.log('[JOR] resolve', myResolve === resolve)
            if (!res.ok) {
                reject(res);
            } else {
                res.text()
                    .then(resolve)
                    .catch(reject)
            }
        }).catch(reject);
    })
    return operation
}
  • As we don't know what `authOperation` does, im not sure we could answer this. Does it even allow you to return a promise? What does it do with it? It would seem odd that a callback-centric method knows what to do with the return from `fetch`. – Jamiec Mar 28 '23 at 10:47
  • 1
    It also looks like you're doing the [explicit promise construction anti-pattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) if `authOperation` returns a promise. – Jamiec Mar 28 '23 at 10:49
  • The `authOperation` function requires an authentication token and, when it obtains it, it tries to pass it to the function passed as the first parameter. Depending on the response it receives, it might try to request a second authentication token and then call the function passed as the first argument again. After these operations, it calls the function passed as the second argument. It's ugly code, but why doesn't it work? – Jonata Ornelli Mar 28 '23 at 11:12
  • Can you include the code for `authOperation` - again hard to answer without seeing it. Also what exactly "doesnt work"? – Jamiec Mar 28 '23 at 11:37
  • Thank you for the time you have dedicated to me. I can't understand what's happening, so I can't explain myself. I tried to write a similar code in a Codepen, but I couldn't reproduce the error that is happening to me. I really can't understand, I'm sorry. I'm trying to adapt the Microsoft Outlook Add-in SSO template example to my scenario with my C# Azure Function backend with little success. I'll try to think about it some more and maybe modify my question. Thanks again. – Jonata Ornelli Mar 28 '23 at 14:44

0 Answers0