0

I have a function getPackageName, and inside this function i defined a promise , i need to save the value of this promise and return the value outside , so whenever i call getPackageName,it will return promise result value ...

    getPackageName(modelName){
     let modelReq = new Ext.Promise(function(resolve, reject){
                        Ext.Ajax.request({
                            url: `db/code/pkgname?tablename=${modelName}`,
                            method: 'GET',
                            callback: function(options, success, response){
                            if (success){
                            if (response){
                                    resolve(response);
                                }
                                else{
                                    console.log("No response from server");
                                }
                            }
                        });
                    });

                    modelReq.then(res){
                            return res.res
                            }

    }

it's not working as expected , and whenever i call getPackageName, it will return undefined . Any help would be appreciated for sure .

Jaydeep
  • 1,686
  • 1
  • 16
  • 29
Shah.m
  • 11
  • 6
  • Before `modelReq.then(res){`, add `return`. Then use `getPackageName("?").then(packageName => { ... });` –  May 29 '19 at 08:45
  • @ChrisG thanks for the tip , so this `return` should be inside `Ext.Promise` ? – Shah.m May 29 '19 at 08:49
  • @ChrisG can you please provide an example ? i got confused a bit , appreciate your help – Shah.m May 29 '19 at 08:52
  • currently, `getPackageName()` isn't returning anything. You need to return the Promise: `return modelReq.then(res => res.res);` –  May 29 '19 at 08:54
  • @ChrisG after adding this return , whenever i call `getPackageName()` it will return `Promise {}` – Shah.m May 29 '19 at 09:20
  • Yes, that's because `getPackageName()` returns a Promise. Either use `async/await` or `.then()` (like I showed in my first comment...) –  May 29 '19 at 09:42
  • @ChrisG got it , thanks , so when i want to call the function i should use it like `getPackageName("?").then(packageName => { ... });` ? and instead of question mark , what should i use ? sorry im a bit new to promises concepts – Shah.m May 29 '19 at 09:50
  • The parameter is called `modelName`, and it gets inserted into the Ajax URL as `tablename`. So if you want to request `db/code/pkgname?tablename=users` you need `getPackageName("users").then(result => ...)` This is your own code I'm explaining to you, shouldn't you know what to pass to `getPackageName()`? –  May 29 '19 at 09:54
  • @ChrisG thanks , i will try this logic based on your explanations . – Shah.m May 29 '19 at 09:58
  • @ChrisG i tried to save it in a variable , but still it gives me that pending . `let modelPackage = getPackageName(modelName).then(result => {return result});` – Shah.m May 29 '19 at 10:34
  • This is a well-solved standard problem. To get the result right away, you need to use `async/await`. See here: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call –  May 29 '19 at 10:35
  • @ChrisG thanks for all your help. i will check it – Shah.m May 29 '19 at 10:44

0 Answers0