# Async and callback not working in plugin server action

**URL:** <https://forum.bubble.io/t/async-and-callback-not-working-in-plugin-server-action/236346>\
**Category:** Plugins\
**Created:** [December 9, 2022, 9:57am UTC](https://forum.bubble.io/t/async-and-callback-not-working-in-plugin-server-action/236346 "2022-12-09T09:57:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nocode4](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nocode4](https://forum.bubble.io/u/nocode4)\
**Post date:** [December 9, 2022, 9:57am UTC](https://forum.bubble.io/t/async-and-callback-not-working-in-plugin-server-action/236346/1 "2022-12-09T09:57:55Z")

</div>

I have a problem with the code of one of my plugins. I would like to make asynchronous calls and use callbacks but it doesn’t seem to work. My callbacks are never executed.  
The code below is working on a node v14 but not on the plugin. I can’t debug it very well but when i had an alert in the then arrown function it’s never executed (i don’t have the error alert is not defined).  
For example:  
indent preformatted text by 4 spaces  
const sftpClient = require(‘ssh2-sftp-client’);  
const { exit } = require(‘process’);  
const https = require(‘https’);  
let remote = ‘FOLDER/youhou.json’;  
let data = ‘YOURURL’;

```
	const sftp = new sftpClient();
     sftp.connect({
        host: "YOUR HOST",
        port: "PORT",
        username: 'USERNAME',
        password: 'PASSWORD'
    }).then(() => {
        https.get(data, async (stream) => {
             sftp.put(stream, remote).then(() => {
                exit(0);
            });
            
        })
    });
```

---

<div class="post-metadata">

**Author:** ![jared.gibb](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/jared.gibb/32/123329_2.png) [@jared.gibb](https://forum.bubble.io/u/jared.gibb)\
**Post date:** [December 9, 2022, 11:30am UTC](https://forum.bubble.io/t/async-and-callback-not-working-in-plugin-server-action/236346/2 "2022-12-09T11:30:13Z")

</div>

You may need to use the context.async function to complete your async calls.

.then() _should_ work

You’re not returning anything from the SSA. How do you know it’s not working?

---

<div class="post-metadata">

**Author:** ![nocode4](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nocode4](https://forum.bubble.io/u/nocode4)\
**Post date:** [December 9, 2022, 12:29pm UTC](https://forum.bubble.io/t/async-and-callback-not-working-in-plugin-server-action/236346/3 "2022-12-09T12:29:27Z")

</div>

Because i can’t see the file on the FTP even if the code is working locally.  
Why do you mean by context.async? this? [Asynchronous context tracking | Node.js v19.2.0 Documentation](https://nodejs.org/api/async_context.html)  
I have also tried this But it’s also only working locally not on Bubble  
const sftpClient = require(‘ssh2-sftp-client’);  
const https = require(‘https’);  
const sftp = new sftpClient();  
let data = FILEURL;  
let remote = YOURFOLDER;  
const toto = new Promise(function (resolve, reject) {  
https.get(data, async (stream) =\> {  
try {  
await sftp.connect({  
host: HOST,  
port: PORT,  
username: USERNAME,  
password: PASSWORD  
});  
console.log(‘Successfully connected to sftp’);  
} catch (error) {  
console.log(error);  
}  
try {  
await sftp.put(stream, remote);  
resolve(‘done 🤘’);  
} catch (err) {  
reject(err);  
console.log(‘failed:’, err);  
}  
});  
});

return deasync(toto);

---

<div class="post-metadata">

**Author:** ![bubble.trouble](https://avatars.discourse-cdn.com/v4/letter/b/7ba0ec/32.png) [@bubble.trouble](https://forum.bubble.io/u/bubble.trouble)\
**Post date:** [December 9, 2022, 1:18pm UTC](https://forum.bubble.io/t/async-and-callback-not-working-in-plugin-server-action/236346/4 "2022-12-09T13:18:37Z")

</div>

> [@Understanding \`context.async\`](http://forum.bubble.io/t/understanding-context-async/53299/12):
>
> Here almost a year later, but I did notice that context.async seems to be working now. Only bumping this incase someone did want to use it; I’d recommend declaring a function within the larger run\_server function: function(properties, context) { //... function myAsyncFunction() { return context.async(async callback =\> { try { let return\_var = "never set in code"; // ... // do your processing here // ... …

---

<div class="post-metadata">

**Author:** ![nicholasrbarrow](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/nicholasrbarrow/32/157879_2.png) [@nicholasrbarrow](https://forum.bubble.io/u/nicholasrbarrow)\
**Post date:** [December 9, 2022, 1:36pm UTC](https://forum.bubble.io/t/async-and-callback-not-working-in-plugin-server-action/236346/5 "2022-12-09T13:36:15Z")

</div>

adding on to this, you should download your IDE of choice (I recomend WebStorm or, for a more simple solution, VS Code). Then, install Node 14 on your computer (this means that your version of node and Bubble’s will be pretty close to the same, so you know that what works on your computer _should_ work on the sever. Debug everything in your IDE, then copy and paste to the server.

I’ve noticed that .then(…) and also async/await work perfectly on the front-end/client-side, but fail miserably on the backend–unless you do everything inside context.async.

```
let returnThisToTheUser = context.async(async callback => {
    
    await operation1;
    
    const t = await operation2;
    
    
    const abc = operation3.then( resp => {
        callback(null, resp);
    })
    
    if(err) {
        callback(err, null);
    }
    
})

return {
    "my return var in bubble plugin editor": returnThisToTheUser
}

```

Note that you do not define what `callback` is, Bubble does. It is a function that takes parameters (in this order) `error` and `return value.` If an error occurs, Bubble is supposed to log it. If not, it assigns the `return value` to whatever variable you’re using (in my example, this would be `returnThisToTheUser` (essentially, after the whole async operation occurs, we do `let returnThisToTheUser = ` the second parameter of `callback`).
