You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
561 B

4 years ago
  1. # `whenEmpty()`
  2. The `whenEmpty` method will execute the given callback when the collection is empty:
  3. ```js
  4. const collection = collect([]);
  5. collection.whenEmpty(c => c.push('Mohamed Salah'));
  6. collection.all();
  7. // ['Mohamed Salah']
  8. ```
  9. ```js
  10. const collection = collect(['Sadio Mané']);
  11. collection.whenEmpty(
  12. c => c.push('Mohamed Salah'),
  13. c => c.push('Xherdan Shaqiri'),
  14. );
  15. collection.all();
  16. // [
  17. // 'Sadio Mané',
  18. // 'Xherdan Shaqiri',
  19. // ];
  20. ```
  21. [View source on GitHub](https://github.com/ecrmnn/collect.js/blob/master/src/methods/whenEmpty.js)