---
description: give iterator a chance to perform cleanup actions.
---

# return()

[JS](../..)⟩ [iteration](..) ⟩ [iterator]() ⟩ return()

{% hint style="success" %}
this method is <mark style="color:yellow;">**invoked**</mark>**&#x20;**<mark style="color:red;">**automatically**</mark> when the [..](.. "mention") is <mark style="color:yellow;">**stopped**</mark>**&#x20;**<mark style="color:red;">**prematurel**</mark>, it is where you can perform <mark style="color:orange;">**cleanup**</mark> actions.
{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="success" %}
calling this method tells the [](<> "mention") that <mark style="color:yellow;">**the caller**</mark>**&#x20;**<mark style="color:red;">**does not intend**</mark>**&#x20;**<mark style="color:yellow;">**to make any more**</mark> [next](next "mention") calls and can perform any <mark style="color:orange;">**cleanup**</mark> actions.
{% endhint %}

{% hint style="danger" %}
the <mark style="color:purple;">**return()**</mark> method must return an [iteration-result](../iteration-result "mention") object：

* <mark style="color:yellow;">**properties**</mark> of the returned object are <mark style="color:red;">**ignored**</mark>
* it is an <mark style="color:red;">**error**</mark> to return a <mark style="color:yellow;">**non-object**</mark> value
{% endhint %}
{% endtab %}

{% tab title="💈範例" %}
* [sequence](../iterable/custom/sequence "mention") - custom iterable of numbers.
{% endtab %}

{% tab title="📗 參考" %}
* [ ] JavaScript Tutorial ⟩ [The Essential Guide to JavaScript Iterators](https://www.javascripttutorial.net/es6/javascript-iterator/) ⭐️
{% endtab %}

{% tab title="📘 手冊" %}
* [the iterator protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol)
{% endtab %}
{% endtabs %}
