JavaScript How to return different promises from a function?

A function whose declared type is neither 'void' nor 'any' must return a value.

I have a createOrder function, that is supposed to return a promise. I have database calls in the function, and I'd like to resolve the promise of the createOrder function, when all database actions have finished.

The problem is that the typescript compiler don't let me return the promise from inside the database function returned promise because it thinks that the function declaration (return a Promise) isn't fulfilled:

createOrder(order:Array<Cartitem>, sum:number):Promise<Array<Cartitem>> {
    this.db.put(newOrder)
        .then((r)=>{
           //other operations
           return new Promise<Array<Cartitem>>((resolve) => {
               resolve(order);
           });
        })
        .catch(function (err) {
           console.log('put order:', err);
           // return new Promise<Array<Cartitem>>((reject) => {
           //   reject(order);
           // });
        });
}

I can return a Promise at the end of the function, but that is not good for anything of course except for the compiler. Should I use a simple callback here?

Answer:1

You can return the Promise as your function's return value, and resolve it in your inner promise callbacks:

createOrder(order:Array<Cartitem>, sum:number):Promise<Array<Cartitem>>  
{
   return new Promise<Array<Cartitem>>((resolve) => {
      this.db.put(newOrder)
        .then((r)=>{
            //other operations
           resolve(order);
        })
        .catch((err) => {
           console.log('put order:', err);
           reject(err);
        });
   });
}
Answer:2

Depending on what you're doing in "other operations," you either use the promise created by calling then on this.db.put's promise if you can:

createOrder(order:Array<Cartitem>, sum:number):Promise<Array<Cartitem>> {
    return this.db.put(newOrder)
        .then((r)=>{
           //other operations
           return order;                   // Resolves the one created by `then`
        })
        .catch(function (err) {
           console.log('put order:', err);
           throw err;                      // Rejects the one created by `then`
        });
    });
}

...or if you can't for some reason, create your promise earlier, and make your call to this.db.put inside its initialization callback:

createOrder(order:Array<Cartitem>, sum:number):Promise<Array<Cartitem>> {
    return new Promise<Array<Cartitem>>((resolve) => {
        this.db.put(newOrder)
            .then((r)=>{
               //other operations
               resolve(order);
            })
            .catch(function (err) {
               console.log('put order:', err);
               reject(err); // You had `order` here, but presumably it would be `err`
            });
    });
}
Answer:3

try something like

createOrder(order:Array<Cartitem>, sum:number):Promise<Array<Cartitem>> {
    return this.db.put(newOrder)
        .then((r)=>{
           //other operations
           return new Promise<Array<Cartitem>>((resolve) => {
               resolve(order);
           });
        })
        .catch(function (err) {
           console.log('put order:', err);
           // return new Promise<Array<Cartitem>>((reject) => {
           //   reject(order);
           // });
           return null
        });

}
Answer:4

I'm trying to split text the following like on spaces: var line = "Text (what is)|what's a story|fable called|named|about {Search}|{Title}" but I want it to ignore the spaces within parentheses. ...

I'm trying to split text the following like on spaces: var line = "Text (what is)|what's a story|fable called|named|about {Search}|{Title}" but I want it to ignore the spaces within parentheses. ...

  1. split string spaces java
  2. split string spaces python
  3. split string spaces c++
  4. split string spaces javascript
  5. split string spaces php
  6. split string spaces regex
  7. split string without spaces python
  8. split string without spaces into words java
  9. split string without spaces java
  10. split string multiple spaces python
  11. split string without spaces javascript
  12. split string by spaces python 3
  13. split string by spaces golang
  14. java split string spaces into array
  15. powershell split string spaces
  16. split string multiple spaces java
  17. split string by spaces matlab
  18. split string by spaces sql

I have a template Hello, ${user.name} stored in a variable. I am reading this from an external file using fs.read. Now, obviously when I attach to the innerHTML of a target div, it shows the string ...

I have a template Hello, ${user.name} stored in a variable. I am reading this from an external file using fs.read. Now, obviously when I attach to the innerHTML of a target div, it shows the string ...

I have a search form(search form) rendered inside a modal window(angular ui bootstrap modal). The input fields hold values that update my ng-model on submit. <script type="text/ng-template" id="...

I have a search form(search form) rendered inside a modal window(angular ui bootstrap modal). The input fields hold values that update my ng-model on submit. <script type="text/ng-template" id="...

  1. angular canactivate
  2. angular can't resolve all parameters
  3. angular cannot find module
  4. angular cannot read property of undefined
  5. angular candeactivate
  6. angular cannot get /
  7. angular canvas
  8. angular cancel http request
  9. angular cannot match any routes
  10. angular cannot find control with name
  11. angular canactivate redirect
  12. angular cannot find name 'require'
  13. angular cannot determine the module for class
  14. angular canload
  15. angular cannot access before initialization
  16. angular canactivatechild
  17. angular candeactivate example
  18. angular cannot set property of undefined
  19. angular cannot find control with path
  20. angular cannot read property of null

I have a webpage with a variety of text snippets enclosed in <span class="x"></span> tags. I'd like to generate an ordered list of each such snippet. Straightforward enough. The wrinkle: ...

I have a webpage with a variety of text snippets enclosed in <span class="x"></span> tags. I'd like to generate an ordered list of each such snippet. Straightforward enough. The wrinkle: ...

  1. extract text from image
  2. extract text from pdf
  3. extract text from excel cell
  4. extract text from pdf python
  5. extract text from pdf image
  6. extract text from image python
  7. extract text from html
  8. extract text from cell
  9. extract text from website
  10. extract text from html python
  11. extract text from powerpoint
  12. extract text from image mac
  13. extract text from video
  14. extract text from word document
  15. extract text from image onenote
  16. extract text from string python
  17. extract text from youtube video
  18. extract text from png
  19. extract text from image android
  20. extract text from webpage